在GO中调用外部命令 [英] Calling an external command in GO

查看:141
本文介绍了在GO中调用外部命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在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.

推荐答案

您需要使用 exec package :使用命令启动一个命令并使用 Run 等待完成。

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 Ouput instead of Run.

这篇关于在GO中调用外部命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆