Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

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.

PreviousWriting FilesNextFile Paths

Last updated 1 year ago

Was this helpful?