duan1989643
duan1989643
2013-08-24 16:31
浏览 30
已采纳

在GO中调用外部命令

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.

图片转代码服务由CSDN问答提供 功能建议

如何在GO中调用外部命令? 我需要调用外部程序并等待其完成 执行。 在执行下一条语句之前。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dra11767
    dra11767 2013-08-24 16:34
    已采纳

    You need to use the exec package : start a command using Command and use Run to wait for completion.

    cmd := exec.Command("yourcommand", "some", "args")
    if err := cmd.Run(); err != nil { 
        fmt.Println("Error: ", err)
    }   
    

    If you just want to read the result, you may use Output instead of Run.

    点赞 评论

相关推荐