I know how to capture the output of a exec.Command but I would like to also stream it to stdout while still capturing it.
Thanks for any input!
package main
import (
"bytes"
"fmt"
"os/exec"
)
func main() {
cmd := exec.Command("ls")
var out bytes.Buffer
cmd.Stdout = &out
cmd.Run()
fmt.Println(out.String())
}
io.TeeReader()orio.MultiWriter(). For an example, see What is the difference between io.TeeRearder and io.Copy?lsmay be a toy example here. But if not, it's preferred to use nativeGoAPIs (like os.ReadDir) rather than launching an external executable.