Working with Unix pipes
Unix pipes are useful when passing the output of one program to the input of another. For example, take a look at this:
$ echo "test case" | wc -l 1
In a Go application, the left-hand side of the pipe can be read in using os.Stdin
and acts like a file descriptor. To demonstrate this, this recipe will take an input on the left-hand side of a pipe and return a list of words and their number of occurrences. These words will be tokenized on white space.
Getting ready
Refer to the Getting ready section's steps in the Using command-line flags recipe.
How to do it...
These steps cover writing and running your application:
- From your terminal/console application, create a new directory called
chapter2/pipes
and navigate to that directory. - Copy tests from https://github.com/agtorre/go-cookbook/tree/master/chapter2/pipes, or use this as an exercise to write some of your own code!
- Create a file called
pipes.go
with the following contents:
package main import ( ...