golang exec.Command程序的奇怪行为

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

Strange behaviour of golang exec.Command program

问题

我有这样的代码:

func main() {
    s := "foobar"
    cmd := exec.Command("wc", "-l")
    stdin, err := cmd.StdinPipe()
    if err != nil {
        log.Panic(err)
    }
    stdout, err := cmd.StdoutPipe()
    if err != nil {
        log.Panic(err)
    }
    err = cmd.Start()
    if err != nil {
        log.Panic(err)
    }
    io.Copy(stdin, bytes.NewBufferString(s))
    stdin.Close()
    io.Copy(os.Stdout, stdout)
    err = cmd.Wait()
    if err != nil {
        log.Panic(err)
    }
}

它的输出是:

0

但是当我做一个简单的修改时:

func main() {
    runWcFromStdinWorks("aaa\n")
    runWcFromStdinWorks("bbb\n")
}

func runWcFromStdinWorks(s string) {
    cmd := exec.Command("wc", "-l")
    stdin, err := cmd.StdinPipe()
    if err != nil {
        log.Panic(err)
    }
    stdout, err := cmd.StdoutPipe()
    if err != nil {
        log.Panic(err)
    }
    err = cmd.Start()
    if err != nil {
        log.Panic(err)
    }
    io.Copy(stdin, bytes.NewBufferString(s))
    stdin.Close()
    io.Copy(os.Stdout, stdout)
    err = cmd.Wait()
    if err != nil {
        log.Panic(err)
    }
}

它可以工作,但是为什么?它只是调用了一个方法,为什么第一个版本不起作用?

英文:

I have such code:

func main() {
        s := "foobar"
        cmd := exec.Command("wc", "-l")
        stdin, err := cmd.StdinPipe()
        if err != nil {
                log.Panic(err)
        }
        stdout, err := cmd.StdoutPipe()
        if err != nil {
                log.Panic(err)
        }
        err = cmd.Start()
        if err != nil {
                log.Panic(err)
        }
        io.Copy(stdin, bytes.NewBufferString(s))
        stdin.Close()
        io.Copy(os.Stdout, stdout)
        err = cmd.Wait()
        if err != nil {
                log.Panic(err)
        }
}

and its output is:

0

But when I will do simple modification:

func main() {
        runWcFromStdinWorks("aaa\n")
        runWcFromStdinWorks("bbb\n")
}

func runWcFromStdinWorks(s string) {
        cmd := exec.Command("wc", "-l")
        stdin, err := cmd.StdinPipe()
        if err != nil {
                log.Panic(err)
        }
        stdout, err := cmd.StdoutPipe()
        if err != nil {
                log.Panic(err)
        }
        err = cmd.Start()
        if err != nil {
                log.Panic(err)
        }
        io.Copy(stdin, bytes.NewBufferString(s))
        stdin.Close()
        io.Copy(os.Stdout, stdout)
        err = cmd.Wait()
        if err != nil {
                log.Panic(err)
        }
}

It works, but why? Its just calling method why first version is not working?

答案1

得分: 4

在第一个示例中,字符串s没有换行符,这导致wc -l返回0。你可以通过以下方式观察到这种行为:

$ echo -n hello | wc -l
0
英文:

The string s in the first example does not have a new line, which causes wc -l to return 0. You can see this behavior by doing:

$ echo -n hello | wc -l
0

huangapple
  • 本文由 发表于 2015年11月19日 14:35:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/33796826.html
匿名

发表评论

匿名网友

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

确定