What I want to do is like:
cmd := exec.Command(someCommand)
cmd.Stdout = os.Stdout
cmd.Run()
save(os.Stdout)
Because this command takes a long time executing, I want to print results on the screen immediately. So I do not want to use result := cmd.Output() fmt.Print(result)
to save the output and then print
Usa a MultiWriter:
cmd := exec.Command(someCommand)
var buf bytes.Buffer
cmd.Stdout = io.MultiWriter(os.Stdout, &buf)
cmd.Run()
save(buf.Bytes()) // Bytes() returns a []byte containing the stdout from the commmand.