How would I correctly redirect the output of exec.Cmd.Start so I can view its progress

  Kiến thức lập trình

I wrote this go program to help me asynchronously execute some long running tests that I have been running synchronously until now. It works pretty well, except for the real time progress updates I am hoping to get during execution.

logFile, err := os.Create(filepath)
if err != nil {....}
defer logFile.Close()
fileWriter := bufio.NewWriter(logFile)

errFile, err := os.Create(filepath + ".err")
if err != nil {....}
defer errFile.Close()
errWriter := bufio.NewWriter(errFile)

cmd := exec.Command(app, arg1, arg2, arg3)
var stdoutBuf, stderrBuf bytes.Buffer
cmd.Stdout = io.MultiWriter(fileWriter, &stdoutBuf)
cmd.Stderr = io.MultiWriter(errWriter, &stderrBuf)

err = cmd.Start()
if err != nil {...return}
err = cmd.Wait()
if err != nil {...return}

outStr, errStr := string(stdoutBuf.Bytes()), string(stderrBuf.Bytes())
_, _ = logger.WriteString(outStr)
_, _ = logger.WriteString(errStr)

This works just fine if I replace fileWriter and errWriter with os.Stdout and os.Stderr but I am hoping to redirect the same output to the two files I create. I’ve read some discussions about how I would need to create a custom writer since this one is buffered(and os.stdout is not), does anyone happen to have an example of such a custom writer?