###### add-line-numbers.go ```go package main import ( "bufio" "fmt" "os" ) func main() { // Buffered input that splits input on lines. input := bufio.NewScanner(os.Stdin) // Buffered output. output := bufio.NewWriter(os.Stdout) lineNo := 0 // Scan until EOF (no more input). for input.Scan() { text := input.Text() lineNo++ s := fmt.Sprintf("%03d %s\n", lineNo, text) // It would be simpler to just use fmt.Println, // but want to emphasize piping stdin to stdout // explicitly. // Intentionally ignoring return values. _, _ = output.WriteString(s) } // Always explicitly flush remaining buffered output. _ = output.Flush() } ``` The essence of a command that can be used in a pipe operation is that it reads from `stdin` and writes to `stdout`. This example reads a line at a time from `stdin` and writes it back out to `stdout` with each line prefixed with the line number. ``` $ cat command-pipe.go | go run command-pipe.go 001 package main 002 003 import ( 004 "bufio" 005 "fmt" 006 "os" 007 ) 008 009 func main() { 010 011 // Buffered input that splits input on lines. 012 input := bufio.NewScanner(os.Stdin) 013 014 // Buffered output. 015 output := bufio.NewWriter(os.Stdout) 016 017 lineNo := 0 018 019 // Scan until EOF (no more input). 020 for input.Scan() { 021 text := input.Text() 022 lineNo++ 023 s := fmt.Sprintf("%03d %s\n", lineNo, text) 024 025 // It would be simpler to just use fmt.Println, 026 // but want to emphasize piping stdin to stdout 027 // explicitly. 028 // Intentionally ignoring return values. 029 _, _ = output.WriteString(s) 030 031 } 032 033 // Always explicitly flush remaining buffered output. 034 _ = output.Flush() 035 036 } ``` --- Next: [[10. Base64 Command]]