通过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窗口的详细内容,更多请关注其它相关文章!