Line filters
Context
Section titled “Context”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.
Example
Section titled “Example”A filter that converts each line to uppercase.
Code example
Section titled “Code example”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) }}Output (if input is “hello\nworld”)
Section titled “Output (if input is “hello\nworld”)”HELLOWORLD