Questions like how to take input in golang, golang get user input, go read user input, or user input golang all point at os.Stdin together with fmt or bufio. The fmt functions split on whitespace and decode into variables; bufio is better when you need a full line or want to control delimiters. This article covers golang read input and parsing multiple values in one go—related to variadic-style APIs like fmt.Scanln (see variadic functions in Go).
Tested with Go 1.24 on Linux.
golang read input with fmt.Scan, Scanln, and Scanf
fmt.Scan, fmt.Scanln, and fmt.Scanf read from standard input, skipping leading spaces and splitting fields on whitespace. Scanln stops at a newline; Scan can leave a trailing newline in the buffer, which surprises people learning how to get user input in go.
package main
import "fmt"
func main() {
var first, last string
var age int
fmt.Println("Enter first name, last name, and age (space-separated):")
n, err := fmt.Scan(&first, &last, &age)
if err != nil {
fmt.Println("read error:", err)
return
}
fmt.Printf("read %d fields: %s %s (%d)\n", n, first, last, age)
}Run locally, then type Ada Lovelace 36 and press Enter. The field order in the Scan call must match the order you type.
For typed patterns on one line, Scanf uses a format string like printf:
package main
import "fmt"
func main() {
var base, height float64
fmt.Println("Enter base and height (two floats, space-separated):")
if _, err := fmt.Scanf("%f %f", &base, &height); err != nil {
fmt.Println("read error:", err)
return
}
fmt.Printf("area=%.2f\n", 0.5*base*height)
}Use %f or %g for floats; width modifiers such as %2f are rarely what you want for scanning.
Line-based input golang (bufio and os.Stdin)
When input is a sentence or you need stable “one line = one string” behavior, attach a bufio.Scanner to os.Stdin (or a bufio.Reader and ReadString('\n')):
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
fmt.Println("Describe your project in one line, then press Enter:")
line, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
fmt.Println("read error:", err)
return
}
line = strings.TrimSuffix(line, "\n")
fmt.Println("You wrote:", line)
}For many lines until EOF, loop scanner.Scan() and use scanner.Text(); set scanner.Split(bufio.ScanWords) if you want token-by-token reading instead of whole lines.
Summary
Golang user input from the terminal is stdin: fmt.Scan, Scanln, and Scanf answer how to take input in golang when values are short and whitespace-separated; they are the usual get user input golang path for quick CLIs. golang read input for full lines belongs in bufio with ReadString or a Scanner. Watch newline interactions between Scan and later reads, and validate error returns from every read.
References
- Package fmt (
Scan,Scanln,Scanf) - Package bufio

