Skip to content

Go Line Filters

A line filter is a common type of program that reads input from stdin, processes it line-by-line, and then prints a result to stdout. Examples include grep or sed.

Basic Example: Uppercase Filter

This program reads text from stdin, converts it to uppercase, and prints it.

package main

import (
    "bufio"
    "fmt"
    "os"
    "strings"
)

func main() {
    // 1. Create a scanner to read from standard input
    scanner := bufio.NewScanner(os.Stdin)

    for scanner.Scan() {
        // 2. Process each line
        ucl := strings.ToUpper(scanner.Text())

        // 3. Output the result
        fmt.Println(ucl)
    }

    // 4. Check for errors during scanning
    if err := scanner.Err(); err != nil {
        fmt.Fprintln(os.Stderr, "error:", err)
        os.Exit(1)
    }
}

How to use it

You can use the pipe (|) operator in your terminal to feed data into your Go program:

1
2
3
4
5
6
# Compile the program
go build uppercase.go

# Pipe another command's output into it
echo "hello go" | ./uppercase
# Output: HELLO GO

Common Filter Ideas

  1. Grep: Only print lines that contain a specific word.
  2. Line Counter: Add a number to the start of every line.
  3. Search and Replace: Replace every occurrence of "foo" with "bar".
  4. Log Parser: Extract only the timestamp and error message from a log file.

Why use Line Filters?

  1. Memory Efficiency: By processing data line-by-line, your program can handle files that are much larger than the available RAM.
  2. Composability: You can chain multiple small filter programs together to solve complex tasks.