Piping between writer and reader
The pipes between processes are the easy way to use the output of the first process as the input of other processes. The same concept could be done in Go, for example, to pipe data from one socket to another socket, to create the tunneled connection. This recipe will show you how to create the pipe with use of the Go built-in library.
How to do it...
- Open the console and create the folder
chapter05/recipe09
. - Navigate to the directory.
- Create the
pipe.go
file with the following content:
package main import ( "io" "log" "os" "os/exec" ) func main() { pReader, pWriter := io.Pipe() cmd := exec.Command("echo", "Hello Go!\nThis is example") cmd.Stdout = pWriter go func() { defer pReader.Close() if _, err := io.Copy(os.Stdout, pReader); err != nil { log.Fatal(err) } }() if err := cmd...