exec.Run - 这个 Go 程序出了什么问题?
这个 Golang 程序不是应该将目录列表输出到 stdout 吗? 它编译正常,但什么也没做。
package main
import "exec"
func main() {
argv := []string{"-la"}
envv := []string{}
exec.Run("ls", argv, envv, "", exec.DevNull, exec.PassThrough, exec.MergeWithStdout)
}
Isn't this Golang program supposed to output a directory listing to stdout?
It compiles ok, but does nothing.
package main
import "exec"
func main() {
argv := []string{"-la"}
envv := []string{}
exec.Run("ls", argv, envv, "", exec.DevNull, exec.PassThrough, exec.MergeWithStdout)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这有效:
this works:
您还可以在本机 go 中使用:
ioutil.ReadDir(dir)
来完成此操作,如下所示:查看可用于 ioutil 和 os 软件包。
You could also do it in native go using:
ioutil.ReadDir(dir)
, like so:Checkout the documentation available for ioutil and os packages.
默认情况下 exec.Command 会将标准输入、输出和错误连接到 /dev/null。因此,您的“ls”命令运行良好,但输出只是被丢弃。 添加: ,那么您的输出将到达您可能期望的位置。
如果您在 exec.Run 调用之前
By default exec.Command will leave standard input, output and error connected to /dev/null. So, your 'ls' command is running fine but the output is just being thrown away. If you add:
before the exec.Run call then your output will go where you probably expect it.
exec.Run 将您的程序替换为它执行的程序——它永远不会返回到您的应用程序。这意味着当“cd”完成时,它将正常退出,唯一的效果应该是更改目录; “ls”永远不会运行。
exec.Run replaces your program with the one it executes -- it never returns to your app. This means that when 'cd' completes, it will exit as normal, and the only effect should be of changing the directory; 'ls' will never run.