How to Read User Input in Go from Stdin

Read user input from stdin in Go using bufio.Scanner to process lines until EOF.

Use bufio.Scanner to read lines from os.Stdin in a loop until input ends. This approach handles buffering automatically and stops cleanly on EOF.

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		fmt.Println(scanner.Text())
	}
	if err := scanner.Err(); err != nil {
		fmt.Fprintln(os.Stderr, "error:", err)
	}
}