」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 為什麼我的GO代碼在使用exec.Command時返回“退出狀態1”?

為什麼我的GO代碼在使用exec.Command時返回“退出狀態1”?

發佈於2025-04-08
瀏覽:232

Why Does My Go Code Return 在go的exec.command

中遇到神秘的“ exit status 1”錯誤時使用gololang exec.command con a dainncopt a Ploint contoct point contoct contip。本簡要指南將提供有效解決錯誤故障排除的見解。

使用stderr使用stderr進行增強錯誤消息 cmd:= exec.command(“ find”,“”,“/”,“ -maxDepth”,“ 1”,“ -Exec”,“ WC”,“ - c”,“ -c”,“ {}”,“ {}”,“ \\”) var out bytes.buffer var stderr bytes.buffer cmd.stdout =&out cmd.stderr =&stderr 現在,當執行命令並發生錯誤時,錯誤消息將寫入stderr buffer而不是通用的“退出狀態1。 exec.command(“ find”,“/”,“ -maxDepth”,“ 1”,“ -Exec”,“ WC”,“ -C”,“ {}”,“ \\”) var out bytes.buffer var stderr bytes.buffer cmd.stdout =&out cmd.stderr =&stderr err:= cmd.run() 如果err! = nil { fmt.println(fmt.sprint(err)“:” stderr.string()) 返回 } fmt.println(“結果:” out.string())

在執行後,代碼將顯示以下詳細錯誤消息:

附加註意事項

有些命令可能會打印出錯誤的錯誤,而另一些命令可能會將錯誤打印到STDERR,但仍然返回成功的退出狀態(0)。因此,可能有必要調整代碼以符合您執行的特定命令。

最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3