Home >Backend Development >Golang >Why Does exec.Command in Go Return 'Exit Status 1' and How Can I Get More Information?
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.
The above is the detailed content of Why Does exec.Command in Go Return 'Exit Status 1' and How Can I Get More Information?. For more information, please follow other related articles on the PHP Chinese website!