Processing output from a child process using a pipe
Remember that the standard output and standard error streams of a process are concurrent streams. If the output generated by the child process is potentially unbounded, you can work with it in a separate goroutine. This recipe shows how.
How to do it...
A few words about pipes. A pipe is a stream-based analog of a Go channel. It is a first-in, first-out (FIFO) communication mechanism with two ends: a writer and a reader. The reader side blocks until the writer writes something, and the writer side blocks until the reader reads from it. When you are done with a pipe, you close the writer side, which also closes the reader side of the pipe. This happens when a child process terminates. If you close the reader side of a pipe and then write to it, the program will receive a signal and possibly terminate. This happens if the parent program terminates before the child does.
- Create the command, and get its
StdoutPipe
:ctx, cancel...