fmt.Scan() / Scanf() / Scanln()
fmt.Scan(), fmt.Scanf(), and fmt.Scanln() are functions that read values from standard input (stdin) in Go. You use them in CLI programs that accept input from the user.
Syntax
import "fmt"
// Reads values separated by spaces or newlines.
n, err := fmt.Scan(&var1, &var2)
// Reads values according to a format string.
n, err := fmt.Scanf("format string", &var1, &var2)
// Reads values from a single line (newline acts as the delimiter).
n, err := fmt.Scanln(&var1, &var2)
// Return values:
// n — number of items successfully read
// err — error (e.g., io.EOF)
Function List
| Function | Description |
|---|---|
| fmt.Scan(a ...any) | Reads values from standard input, separated by spaces or newlines. |
| fmt.Scanf(format, a ...any) | Reads values from standard input according to a format string. |
| fmt.Scanln(a ...any) | Reads values from a single line. Stops reading at a newline. |
| fmt.Fscan(r, a ...any) | Reads values from an io.Reader. |
| fmt.Sscan(str, a ...any) | Reads values from a string (uses a string as the input source). |
Sample Code
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
// Using fmt.Sscan to read values from a string (used here as a runnable example).
// In a real program, use fmt.Scan instead.
// Scan: reads multiple values separated by spaces.
var name string
var age int
input := "Yamada 30"
fmt.Sscan(input, &name, &age) // reads from a string (for testing)
fmt.Printf("Name: %s, Age: %d\n", name, age)
// Scanf: reads values using a format string.
var x, y float64
fmt.Sscanf("3.14 2.72", "%f %f", &x, &y)
fmt.Printf("x=%.2f, y=%.2f\n", x, y)
// Scanln: reads values from a single line.
var word1, word2 string
fmt.Sscanln("Hello World", &word1, &word2)
fmt.Printf("word1=%s, word2=%s\n", word1, word2)
fmt.Println()
// For real CLI input, bufio.Scanner is recommended (it can read a full line including spaces).
scanner := bufio.NewScanner(strings.NewReader("Go Dictionary\n"))
if scanner.Scan() {
fmt.Println("Line read:", scanner.Text())
}
// To read from standard input (uncomment to use):
// scanner := bufio.NewScanner(os.Stdin)
// fmt.Print("Input: ")
// if scanner.Scan() {
// line := scanner.Text()
// fmt.Println("Received:", line)
// }
_ = os.Stdin // suppress unused import error
}
Notes
The fmt.Scan() family of functions is well-suited for reading simple values, but struggles with strings that contain spaces (such as full names). To read an entire line, it is common practice to use bufio.NewScanner(os.Stdin). In competitive programming, combining bufio.NewReader(os.Stdin) with fmt.Fscan() for faster input is also a widely used pattern.
Be careful with how newlines ('\n') are handled in the format string of fmt.Scanf(). Input containing Windows-style line endings (\r\n) can cause unexpected behavior. For production CLI tools, using bufio.Scanner is recommended.
For output functions such as fmt.Println(), see fmt.Println() / Printf(). For buffered I/O, see io.Reader / Writer / bufio.
If you find any errors or copyright issues, please contact us.