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:
Output
Just write something on the terminal after running the .go
file ans see the code at work!
Explanation:
Creating a Scanner:
bufio.NewScanner(os.Stdin)
creates a new scanner to read input from the standard input (keyboard).
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.
Printing Uppercase Lines:
fmt.Println(ucl)
prints the uppercase line to the standard output.
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