Skip to content

Go 执行CMD命令

go中执行命令的库是os.exec

go
func main() {
    cmd := exec.Command("cd", "work")
    err := cmd.Run()
    if err != nil {
        fmt.Println(err)
    }
}

如果需要获取执行命令的输出,可以使用StdoutStderr

go
func main() {
    cmd := exec.Command("cd", "work")
    var stdout, stderr bytes.Buffer
    cmd.Stdout = &stdout
    cmd.Stderr = &stderr
    err := cmd.Run()
    fmt.Println(stdout.String(), stderr.String())
    if err != nil {
        fmt.Println(err)
    }
}