go 执行command命令

2020-06-29 16:04:50 浏览数 (1)

使用exec包command命令,命令之间空格就是多个参数拼接。

代码语言:javascript复制
package main

import (
	"fmt"
	"os/exec"
)

func main() {

	filename := "./const.go"
	cmd := exec.Command("go", "fmt", filename)
	fmt.Println(cmd)
	if out, err := cmd.CombinedOutput(); err != nil {
		fmt.Println(string(out), err)
		return
	}
	fmt.Println("End")

	cmd = exec.Command("docker", "build", "-t", filename)
	fmt.Println(cmd)

}

0 人点赞