Line Filters

This Go code reads input from the standard input (stdin) line by line, converts each line to uppercase, and prints the result. Let's go through the code with inline comments and explanations:

// Importing necessary packages.
import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

// The main function, where the execution of the program begins.
func main() {
	// Creating a new scanner to read input from standard input.
	scanner := bufio.NewScanner(os.Stdin)

	// Looping through each line of input.
	for scanner.Scan() {
		// Convert the text to uppercase.
		ucl := strings.ToUpper(scanner.Text())

		// Print the uppercase text.
		fmt.Println(ucl)
	}

	// Checking for any errors that occurred during scanning.
	if err := scanner.Err(); err != nil {
		// Print the error message to standard error.
		fmt.Fprintln(os.Stderr, "error:", err)
		// Exit the program with a non-zero status code.
		os.Exit(1)
	}
}

Output

Just write something on the terminal after running the .go file ans see the code at work!

Explanation:

  1. Creating a Scanner:

    • bufio.NewScanner(os.Stdin) creates a new scanner to read input from the standard input (keyboard).

  2. Reading and Processing Lines:

    • for scanner.Scan() enters a loop, scanning each line of input.

    • strings.ToUpper(scanner.Text()) converts the text of each line to uppercase.

  3. Printing Uppercase Lines:

    • fmt.Println(ucl) prints the uppercase line to the standard output.

  4. Handling Errors:

    • if err := scanner.Err(); err != nil checks for any errors that occurred during scanning.

    • fmt.Fprintln(os.Stderr, "error:", err) prints the error message to standard error.

    • os.Exit(1) exits the program with a non-zero status code if there is an error.

This code provides a simple way to read input from the user, convert it to uppercase, and print the result. It continues to read lines until there is no more input, and it handles errors that may occur during the scanning process.

Last updated