Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use exec in Go language

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

本篇内容介绍了"Go语言的exec怎么使用"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

Exec 是 os 包中的一个子包,它可用于使用 Go 运行外部命令。Go exec 命令教程展示了如何在 Golang 中执行 shell 命令和程序。

要使用这个包,我们需要按如下方式导入:

import "os/exec"使用 GoLang exec 包运行命令

我们可以运行任何我们希望的命令。就像我们使用 CMD、bash 或其他一些 shell 来运行命令一样,它可以运行这些命令。

这是运行 ls 命令的示例。新建一个 main.go :

package mainimport ( "fmt" "os/exec")func main() { cmd := exec.Command("ls") e := cmd.Run() CheckError(e)}func CheckError(e error) { if e != nil { fmt.Println(e) }}

Run 函数启动指定命令并等待它完成,而 Start 启动指定命令但不等待它完成;我们需要使用Wait with Start。

然后新建一个 go.mod 文件:

$ go mod init main.gogo: creating new go.mod: module main.gogo: to add module requirements and sums: go mod tidy

现在,程序将运行,但我们不会看到控制台的任何输出。原因是命令运行,输出没有发送到标准输出。

$ go run main.go

所以,我们需要修复它。添加下面显示的两行以查看控制台的任何输出。

cmd.Stdout = os.Stdoutcmd.Stderr = os.Stderr

输出将显示当前目录中的文件。

package mainimport ( "fmt" "os" "os/exec")func main() { cmd := exec.Command("ls", "-lah") cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr e := cmd.Run() CheckError(e)}func CheckError(e error) { if e != nil { fmt.Println(e) }}

然后我们再程序,可以看到标准台输出如下的文件:

$ go run main.gototal 16drwxr-xr-x 4 yuzhou_1su staff 128B 5 15 22:56 .drwxr-xr-x 23 yuzhou_1su staff 736B 5 15 22:53 ..-rw-r--r-- 1 yuzhou_1su staff 24B 5 15 22:56 go.mod-rw-r--r-- 1 yuzhou_1su staff 248B 5 15 23:18 main.go

利用直接 ls 直接运行该命令,可以看到结果正确:

$ ls -alhtotal 16drwxr-xr-x 4 yuzhou_1su staff 128B 5 15 22:56 .drwxr-xr-x 23 yuzhou_1su staff 736B 5 15 22:53 ..-rw-r--r-- 1 yuzhou_1su staff 24B 5 15 22:56 go.mod-rw-r--r-- 1 yuzhou_1su staff 248B 5 15 23:18 main.go为不同的操作系统指定命令

我们可以指定针对不同操作系统运行不同的命令(例如 Linux 上的 bash 命令)。这是一个例子。

if runtime.GOOS == "linux" { cmd = exec.Command("ls")}

为此,我们还需要导入运行时包。

要查看所有可能的操作系统,我们可以运行 go tool dist list ,它将显示所有可能的操作系统和 ARCH 组合。

Go exec 命令捕获输出

输出运行命令并返回其标准输出:

package mainimport ( "fmt" "log" "os/exec")func main() { out, err := exec.Command("ls", "-l").Output() if err != nil { log.Fatal(err) } fmt.Println(string(out))}

运行该程序:

$ go run main.gototal 16-rw-r--r-- 1 yuzhou_1su staff 24 5 15 22:56 go.mod-rw-r--r-- 1 yuzhou_1su staff 180 5 15 23:33 main.goGo cmd.StdinPipe

管道允许我们将一个命令的输出发送到另一个命令。 StdinPipe 返回一个管道,该管道将在命令启动时连接到命令的标准输入。

package mainimport ( "fmt" "io" "log" "os/exec")func main() { cmd := exec.Command("cat") stdin, err := cmd.StdinPipe() if err != nil { log.Fatal(err) } go func() { defer stdin.Close() io.WriteString(stdin, "an old falcon") }() out, err := cmd.CombinedOutput() if err != nil { log.Fatal(err) } fmt.Printf("%s\n", out)}

在代码示例中,我们将字符串写入 goroutine 内的标准输入。

cmd := exec.Command("cat")

cat 命令将给定的文件连接到标准输出。当没有给定文件或带有 - 时,该命令读取标准输入并将其打印到标准输出。

stdin, err := cmd.StdinPipe()

我们得到 cat 命令的标准输入管道。

go func() { defer stdin.Close() io.WriteString(stdin, "an old falcon")}()

在 goroutine 内部,我们将一个字符串写入标准输入管道。

$ go run stdinpipe.go an old falconGo cmd.StdoutPipe

StdoutPipe 返回一个管道,该管道将在命令启动时连接到命令的标准输出。

package mainimport ( "fmt" "io/ioutil" "log" "os/exec" "strings")func upper(data string) string { return strings.ToUpper(data)}func main() { cmd := exec.Command("echo", "an old falcon") stdout, err := cmd.StdoutPipe() if err != nil { log.Fatal(err) } if err := cmd.Start(); err != nil { log.Fatal(err) } data, err := ioutil.ReadAll(stdout) if err != nil { log.Fatal(err) } if err := cmd.Wait(); err != nil { log.Fatal(err) } fmt.Printf("%s\n", upper(string(data)))}

该示例通过管道读取 echo 命令的输出并将其转换为大写字母。

cmd := exec.Command("echo", "an old falcon")

要运行的命令是带有单个字符串参数的 echo 命令。

stdout, err := cmd.StdoutPipe()

我们得到标准输出管道。

if err := cmd.Start(); err != nil { log.Fatal(err)}

该命令使用 Start 函数执行;它不会等待它完成。

data, err := ioutil.ReadAll(stdout)

我们从管道中读取数据。

if err := cmd.Wait(); err != nil { log.Fatal(err)}

Wait 等待命令退出并等待任何复制到 stdin 或从 stdout 或 stderr 复制完成。它在看到命令退出后关闭管道。

运行该程序:

$ go run stdoutpipe.go AN OLD FALCON"Go语言的exec怎么使用"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report