更新时间:2023-11-30 10:44:28
这个答案不代表 Go 标准库的当前状态.请查看@Lourenco 的回答以获取最新方法!
This answer does not represent the current state of the Go standard library. Please take a look at @Lourenco's answer for an up-to-date method!
您的示例实际上并未从标准输出读取数据.这对我有用.
Your example does not actually read the data from stdout. This works for me.
package main
import (
"fmt"
"exec"
"os"
"bytes"
"io"
)
func main() {
app := "/bin/ls"
cmd, err := exec.Run(app, []string{app, "-l"}, nil, "", exec.DevNull, exec.Pipe, exec.Pipe)
if (err != nil) {
fmt.Fprintln(os.Stderr, err.String())
return
}
var b bytes.Buffer
io.Copy(&b, cmd.Stdout)
fmt.Println(b.String())
cmd.Close()
}