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

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

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

问题

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

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)
}

答案1

得分: 4

这段代码可以工作:

package main
import "exec"
 
func main() {
  cmd, err := exec.Run("/bin/ls", []string{"/bin/ls", "-la"}, []string{}, "", exec.DevNull, exec.PassThrough, exec.PassThrough)
  if (err != nil) {
    return
  }
  cmd.Close()
}
英文:

this works:

package main
import "exec"
 
func main() {
  cmd, err := exec.Run("/bin/ls", []string{"/bin/ls", "-la"}, []string{}, "", exec.DevNull, exec.PassThrough, exec.PassThrough)
  if (err != nil) {
    return
  }
  cmd.Close()
}

答案2

得分: 2

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

//listdir.go
package main

import (
	"os"
	"io/ioutil"
	"fmt"
)

func ListDir(dir string) ([]os.FileInfo, error) {
	return ioutil.ReadDir(dir)
}

func main() {
	dir := "./"
	if len(os.Args) > 1 {
		dir = os.Args[1]
	}
	fi, err := ListDir(dir)
	if err != nil {
		fmt.Println("错误", err)
	}

	for _, f := range fi {
		d := "-"
		if f.IsDir() { d = "d" }
		fmt.Printf("%s %o %d %s %s\n", d, f.Mode() & 0777, f.Size(), f.ModTime().Format("Jan 2 15:04"), f.Name())
	}
}

请查看ioutilos包的文档。

英文:

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

//listdir.go
package main

import (
	"os"
	"io/ioutil"
	"fmt"
)

func ListDir(dir string) ([]os.FileInfo, error) {
	return ioutil.ReadDir(dir)
}

func main() {
	dir := "./"
	if len(os.Args) > 1 {
		dir = os.Args[1]
	}
	fi, err := ListDir(dir)
	if err != nil {
		fmt.Println("Error", err)
	}

	for _, f := range fi {
		d := "-"
		if f.IsDir() { d = "d" }
		fmt.Printf("%s %o %d %s %s\n", d, f.Mode() & 0777, f.Size(), f.ModTime().Format("Jan 2 15:04"), f.Name())
	}
}

Checkout the documentation available for ioutil and os packages.

答案3

得分: 1

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

cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
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:

cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
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:

确定