执行命令行函数

package main
import (
    "fmt"
    "io/ioutil"
   "os/exec"
    "time"
)

func run(command string)  {
        //linux  
    //cmd := exec.Command("/bin/sh", "-c", "ping 127.0.0.1")

    //window
    cmd := exec.Command("cmd", "/C", command )
        if err := cmd.Run(); err != nil {
        fmt.Println("Error: ", err)
        }   
}

func main() {

        run("dir")
    }

在golang里执行windows下的命令行

package main
import(
    "fmt"
    "os/exec"
)

func main(){   
    cmd := exec.Command("cmd", "/C", "del", "D:\\a.txt")

    if err := cmd.Run(); err != nil {
        fmt.Println("Error: ", err)
    }  
}
文档更新时间: 2019-06-07 10:35   作者:月影鹏鹏