Calling an external command in Go

后端 未结 2 973
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-20 21:22

How can I call an external command in GO? I need to call an external program and wait for it to finish execution. before the next statement is executed.

2条回答
  •  醉梦人生
    2021-01-20 21:43

    package main
    
    import (
      "fmt"
      "os/exec"
      "log"
    )
    
    func main() {
      cmd := exec.Command("ls", "-ltr")
      out, err := cmd.CombinedOutput()
      if err != nil {
        log.Fatal(err)
      }
      fmt.Printf("%s\n", out)
    }
    

    Try online

提交回复
热议问题