I have this code to run zsh
and log its output to an output file.
package main
import (
"io"
"os"
"os/exec"
)
func main() {
cmd := exec.Command("zsh")
f, _ := os.Create("log.txt")
multiWriter := io.MultiWriter(os.Stdout, f)
cmd.Stdout = multiWriter
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Run()
}
func haltOnError(err error) {
if err != nil {
panic(err)
}
}
when the program executes, typing ls
will output
foo
bar
while if I let cmd.Stdout = os.Stdout
, it displays correctly as
foo bar
What leads to the differences between os.Stdout
and multiwriter
?