透過go的標準函式庫exec呼叫cmd指令時會閃彈黑窗口,為解決此問題在windows下可以用win32 API 的 WinExec
。
此問題主要出現在帶有UI或無控制台的程式呼叫cmd時。
編譯go時加入參數:
go build -ldflags="-H windowsgui"
package main import ( "errors" " ) import ( "github.com/CodyGuo/win" ) var ( winExecError = map[uint32]string{ 0: "The system is out of memory or resources.", 2: "The .exe file is invalid.", 3: "The specified file was not found.", 11: "The specified path was not found.", } ) func main() { err := execRun("cmd /c start http://www.baidu.com") if err != nil { log.Fatal(err) } } func execRun(cmd string) error { lpCmdLine := win.StringToBytePtr(cmd) //http://baike.baidu.com/link?url=51sQomXsIt6OlYEAV74YZ0JkHDd2GbmzXcKj_4H1R4ILXvQNf3MXIscKnAkSR93e7Fyns4iTmSatDycEb HrXzq ret := win.WinExec(lpCmdLine, win.SW_HIDE) if ret <= 31 { return errors.New(winExecError[ret]) } return nil }
相關文章教學推薦:golang教學
#以上是golang呼叫cmd命令時如何隱藏dos窗口的詳細內容。更多資訊請關注PHP中文網其他相關文章!