什么是执行系统命令的最佳方法

时间:2020-03-03 11:25:24

标签: go

我需要获取类似whois '-i origin XXX' | grep ..."这样的查询的输出,并将其保存在go中的变量中。我该怎么做?我应该通过go在系统上运行命令,还是可以使用任何Web API?

1 个答案:

答案 0 :(得分:0)

您可以在Go中使用os/exec软件包。这将需要一些参数,它将运行系统命令。

示例:

cmd := exec.Command(appname, arg0, arg1, arg2, arg3)   //cmd should be in path variable
stdout, err := cmd.Output()        //Running command
if err != nil {
    fmt.Println(err.Error())         // If error come it will print it
    return
}
fmt.Print(string(stdout))     // Printing the output of the command.

作为参考,请检查-> https://golang.org/pkg/os/exec/

相关问题