exec.Run – 这个Go程序有什么问题?

huangapple go评论130阅读模式
英文:

exec.Run - What's wrong with this Go program?

问题

这个Golang程序不是应该将目录列表输出到标准输出吗?
它可以编译通过,但是没有任何输出。

  1. package main
  2. import "exec"
  3. func main() {
  4. argv := []string{"-la"}
  5. envv := []string{}
  6. exec.Run("ls", argv, envv, "", exec.DevNull, exec.PassThrough, exec.MergeWithStdout)
  7. }
英文:

Isn't this Golang program supposed to output a directory listing to stdout?
It compiles ok, but does nothing.

  1. package main
  2. import "exec"
  3. func main() {
  4. argv := []string{"-la"}
  5. envv := []string{}
  6. exec.Run("ls", argv, envv, "", exec.DevNull, exec.PassThrough, exec.MergeWithStdout)
  7. }

答案1

得分: 4

这段代码可以工作:

  1. package main
  2. import "exec"
  3. func main() {
  4. cmd, err := exec.Run("/bin/ls", []string{"/bin/ls", "-la"}, []string{}, "", exec.DevNull, exec.PassThrough, exec.PassThrough)
  5. if (err != nil) {
  6. return
  7. }
  8. cmd.Close()
  9. }
英文:

this works:

  1. package main
  2. import "exec"
  3. func main() {
  4. cmd, err := exec.Run("/bin/ls", []string{"/bin/ls", "-la"}, []string{}, "", exec.DevNull, exec.PassThrough, exec.PassThrough)
  5. if (err != nil) {
  6. return
  7. }
  8. cmd.Close()
  9. }

答案2

得分: 2

你也可以使用原生的Go语言来完成,使用ioutil.ReadDir(dir),像这样:

  1. //listdir.go
  2. package main
  3. import (
  4. "os"
  5. "io/ioutil"
  6. "fmt"
  7. )
  8. func ListDir(dir string) ([]os.FileInfo, error) {
  9. return ioutil.ReadDir(dir)
  10. }
  11. func main() {
  12. dir := "./"
  13. if len(os.Args) > 1 {
  14. dir = os.Args[1]
  15. }
  16. fi, err := ListDir(dir)
  17. if err != nil {
  18. fmt.Println("错误", err)
  19. }
  20. for _, f := range fi {
  21. d := "-"
  22. if f.IsDir() { d = "d" }
  23. fmt.Printf("%s %o %d %s %s\n", d, f.Mode() & 0777, f.Size(), f.ModTime().Format("Jan 2 15:04"), f.Name())
  24. }
  25. }

请查看ioutilos包的文档。

英文:

You could also do it in native go using: ioutil.ReadDir(dir), like so:

  1. //listdir.go
  2. package main
  3. import (
  4. "os"
  5. "io/ioutil"
  6. "fmt"
  7. )
  8. func ListDir(dir string) ([]os.FileInfo, error) {
  9. return ioutil.ReadDir(dir)
  10. }
  11. func main() {
  12. dir := "./"
  13. if len(os.Args) > 1 {
  14. dir = os.Args[1]
  15. }
  16. fi, err := ListDir(dir)
  17. if err != nil {
  18. fmt.Println("Error", err)
  19. }
  20. for _, f := range fi {
  21. d := "-"
  22. if f.IsDir() { d = "d" }
  23. fmt.Printf("%s %o %d %s %s\n", d, f.Mode() & 0777, f.Size(), f.ModTime().Format("Jan 2 15:04"), f.Name())
  24. }
  25. }

Checkout the documentation available for ioutil and os packages.

答案3

得分: 1

默认情况下,exec.Command会将标准输入、输出和错误连接到/dev/null。所以,你的'ls'命令运行得很好,但输出只是被丢弃了。如果你在exec.Run调用之前添加以下代码:

  1. cmd.Stdin = os.Stdin
  2. cmd.Stdout = os.Stdout
  3. cmd.Stderr = os.Stderr

那么你的输出将会被发送到你预期的位置。

英文:

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:

  1. cmd.Stdin = os.Stdin
  2. cmd.Stdout = os.Stdout
  3. cmd.Stderr = os.Stderr

before the exec.Run call then your output will go where you probably expect it.

答案4

得分: -1

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.

huangapple
  • 本文由 发表于 2010年9月14日 23:04:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/3710167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定