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)
}
}
Reading user input in Go from stdin lets your program wait for a user to type text into the terminal and press Enter. It reads each line one by one and stops automatically when the user signals they are finished (usually by pressing Ctrl+D or Ctrl+Z). Think of it like a digital notepad that waits for you to write a line before showing it back to you.