通过go的标准库exec调用cmd命令时会闪弹黑窗口,为解决此问题在windows下可以用win32 API 的 WinExec。

此问题主要出现在带UI或无控制台的程序调用cmd时。

编译go时加入参数: go build -ldflags=”-H windowsgui”

package main

import (
    "errors"
    "log"
)

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
}
文档更新时间: 2019-06-07 09:48   作者:月影鹏鹏