Skip to content

Line filters

Line filters read input line by line (from stdin or a file) and transform each line. Common in Unix pipelines. Use bufio.Scanner to read lines.

A filter that converts each line to uppercase.

package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
fmt.Println(strings.ToUpper(line))
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
}
Terminal window
HELLO
WORLD