Rumah  >  Artikel  >  pembangunan bahagian belakang  >  Mengapakah exec.Command dalam Go Mengembalikan "Keluar Status 1" dan Bagaimana Saya Boleh Mendapatkan Maklumat Lanjut?

Mengapakah exec.Command dalam Go Mengembalikan "Keluar Status 1" dan Bagaimana Saya Boleh Mendapatkan Maklumat Lanjut?

Patricia Arquette
Patricia Arquetteasal
2024-11-13 13:32:02774semak imbas

Why Does exec.Command in Go Return

Debugging "exit status 1" Error in Golang Exec.Command

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!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn