Output printed twice in 03-live-progress-and-capture-v3.go
vinaycalastry opened this issue · comments
Line 48 of the code prints the output and the error(if any) from the external command's result.
go-cookbook/advanced-exec/03-live-progress-and-capture-v3.go
But I see that the output is printed first.
Then again under "out:"
I couldn't understand which part of the code is printing the first output.
Using: go version go1.11.2 windows/amd64
The commentary is in https://blog.kowalczyk.info/article/wOYk/advanced-command-execution-in-go-with-osexec.html
The example is showing how to connect program's output to stdout/stderr, so that you can see output while the program executes:
stdoutIn, _ := cmd.StdoutPipe()
stderrIn, _ := cmd.StderrPipe()
var errStdout, errStderr error
stdout := io.MultiWriter(os.Stdout, &stdoutBuf)
stderr := io.MultiWriter(os.Stderr, &stderrBuf)
That is the first output.
And how to also capture the output. That is the second output:
outStr, errStr := string(stdoutBuf.Bytes()), string(stderrBuf.Bytes())
fmt.Printf("\nout:\n%s\nerr:\n%s\n", outStr, errStr)
Thank you so much. I read the MultiWriter code more carefully and I think I get it now.