您好,欢迎访问一九零五行业门户网

golang调用cmd命令时如何隐藏dos窗口

通过go的标准库exec调用cmd命令时会闪弹黑窗口,为解决此问题在windows下可以用win32 api 的 winexec。
此问题主要出现在带ui或无控制台的程序调用cmd时。
编译go时加入参数:
go build -ldflags="-h windowsgui"
package mainimport ( "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_4h1r4ilxvqnf3mxiscknaksr93e7fyns4itmsatdycebhrxzq ret := win.winexec(lpcmdline, win.sw_hide) if ret <= 31 { return errors.new(winexecerror[ret]) } return nil}
相关文章教程推荐:golang教程
以上就是golang调用cmd命令时如何隐藏dos窗口的详细内容。
其它类似信息

推荐信息