Streaming Command Output Progress
Question:
How can I stream output from a long-running command, providing intermittent updates rather than a final result?
Answer:
Utilizing the bufio.NewScanner() function with bufio.ScanLines() allows you to read the output of a command line-by-line, streaming the results as they become available. Below is a modified example based on your provided code:
func main() { cmd := exec.Command("go", "run", "child_process.go") stdout, _ := cmd.StdoutPipe() cmd.Start() scanner := bufio.NewScanner(stdout) for scanner.Scan() { fmt.Println(scanner.Text()) log.Printf(scanner.Text()) } cmd.Wait() }
For this to work effectively, the executed command should output the progress using fmt.Println() or log.Printf().
Possible Pitfalls:
Alternative Solutions:
In cases where newline characters are not reliable, alternative methods can be employed:
Caution:
Reading byte-wise or rune-wise may introduce issues with multi-byte UTF-8 encoded characters. A buffer large enough to capture UTF-8 runes should be used.
The above is the detailed content of How to Stream the Progress of a Long-Running Command's Output in Go?. For more information, please follow other related articles on the PHP Chinese website!