golang exec 执行系统命令
? golang ?    2014-09-25 13:17:44    16808    0    0
life   ? golang ?

exec.Command()

最简单的方法:

cmd := exec.Command("/bin/sh/", "-c", "linux命令")

或复杂的, 各个参数都要单独写

cmd := exec.Command("ls", "-l", "-a")

 

package main
   
import (
    "bytes"
    "fmt"
    "os/exec"
)
   
func main() {
    in := bytes.NewBuffer(nil)
    cmd := exec.Command("sh")
    cmd.Stdin = in
    go func() {
        in.WriteString("echo hello world > test.txt\n")
        in.WriteString("exit\n")
    }()
    if err := cmd.Run(); err != nil {
        fmt.Println(err)
        return
    }
}

上一篇: revel 模块静态化

下一篇: golang struct调用方法问题

16808 人读过