Rumah > Artikel > pembangunan bahagian belakang > Mengapakah exec.Command dalam Go Mengembalikan "Keluar Status 1" dan Bagaimana Saya Boleh Mendapatkan Maklumat Lanjut?
When utilizing exec.Command in Golang, encountering the enigmatic "exit status 1" error leaves one scratching their head for specifics. This article aims to illuminate this error's cause and provide a solution for more detailed diagnostics.
Consider the following code:
cmd := exec.Command("find", "/", "-maxdepth", "1", "-exec", "wc", "-c", "{}", "\\") var out bytes.Buffer cmd.Stdout = &out err := cmd.Run()
Executing this code results in the uninformative "exit status 1" error. To remedy this, leverage the Stderr property of the Command object:
var stderr bytes.Buffer cmd.Stderr = &stderr
Upon executing, the error message will become apparent:
exit status 1: find: -exec: no terminating ";" or "+"
Now, equipped with this detailed error, you can address the issue accordingly.
Note: It's worth considering that some commands may redirect error messages to stdout rather than stderr. Additionally, certain commands may print error messages to stderr but still return a zero error code (leading to a nil err in your code). Therefore, it may be necessary to adjust the given solution to accommodate the specific commands you're using.
Atas ialah kandungan terperinci Mengapakah exec.Command dalam Go Mengembalikan "Keluar Status 1" dan Bagaimana Saya Boleh Mendapatkan Maklumat Lanjut?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!