003-golang 调用外部命令

/ go / 没有评论 / 1797浏览

003-golang 调用外部命令

exec包执行外部命令,它将os.StartProcess进行包装使得它更容易映射到stdin和stdout,并且利用pipe连接i/o.

//LookPath在环境变量中查找科执行二进制文件,如果file中包含一个斜杠,则直接根据绝对路径或者相对本目录的相对路径去查找
func LookPath(file string) (string, error) 


f, err := exec.Command("java", "-jar" ,"./bin/apktool_2.2.1.jar" ,"d", "-o",out ,in).Output()
if err != nil {
    fmt.Println(err.Error())
}
fmt.Println(string(f))

在用exec包调用的其他进程后如何关闭结束,可以使用context包的机制进行管理,context包的使用详见:https://godoc.org/context

exec.CommandContext方发实现了context,通过context可以对exec启动的进程结束。

隐藏程序自身黑窗口的方法:go build -ldflags="-H windows"

隐藏子进程黑窗口的方法:

参考链接

  1. golang os/exec 执行外部命令 GO语言运行cmd命令逐行实时输出执行过程
  2. golang exec 执行系统命令