Golang fmt.Scanln function
last modified May 8, 2025
This tutorial explains how to use the fmt.Scanln
function in Go.
We'll cover input basics with practical examples of reading user input.
The fmt.Scanln function is used to read user input in Go. It scans text from standard input, storing successive space-separated values into arguments. It stops scanning at a newline.
In Go, fmt.Scanln
is similar to fmt.Scan
but stops
scanning at a newline. It's commonly used for simple command-line input
scenarios where you want to read until the user presses Enter.
Basic fmt.Scanln example
The simplest use of fmt.Scanln
reads a single value from input.
This example demonstrates reading a string from the user.
Note: The input is read until the first space or newline.
package main import "fmt" func main() { var name string fmt.Print("Enter your name: ") fmt.Scanln(&name) fmt.Printf("Hello, %s!\n", name) }
The program prompts for a name, reads it with fmt.Scanln
, then
greets the user. The &
operator gets the memory address of
the variable.
Reading multiple values
fmt.Scanln
can read multiple values in one call. This example
shows how to read several values separated by spaces.
package main import "fmt" func main() { var firstName, lastName string var age int fmt.Print("Enter first name, last name and age: ") fmt.Scanln(&firstName, &lastName, &age) fmt.Printf("%s %s is %d years old\n", firstName, lastName, age) }
The function reads space-separated values into the provided variables. Input stops when all variables are filled or a newline is encountered.
Handling input errors
We can check the return value of fmt.Scanln
to detect input
errors. This example shows basic error handling.
package main import "fmt" func main() { var number int fmt.Print("Enter a number: ") if _, err := fmt.Scanln(&number); err != nil { fmt.Println("Invalid input:", err) return } fmt.Printf("You entered: %d\n", number) }
The second return value is an error that indicates if scanning failed. This is useful when expecting specific types of input like numbers.
Reading until newline
Unlike fmt.Scan
, fmt.Scanln
stops at newlines.
This example demonstrates the difference in behavior.
package main import "fmt" func main() { var a, b string fmt.Println("Using Scanln (enter two words):") fmt.Scanln(&a, &b) fmt.Printf("Scanln got: %q and %q\n", a, b) fmt.Println("Using Scan (enter two words):") fmt.Scan(&a, &b) fmt.Printf("Scan got: %q and %q\n", a, b) }
fmt.Scanln
will only read input until the first newline.
fmt.Scan
will continue reading from subsequent lines if needed.
Reading mixed data types
fmt.Scanln
can handle different variable types in one call.
This example shows reading a string and a number together.
package main import "fmt" func main() { var product string var price float64 fmt.Print("Enter product name and price: ") if _, err := fmt.Scanln(&product, &price); err != nil { fmt.Println("Error:", err) return } fmt.Printf("%s costs $%.2f\n", product, price) }
The function automatically converts input to the correct type based on the destination variable. Invalid conversions return an error.
Reading into a slice
We can use fmt.Scanln
with slices by reading into individual
elements. This example demonstrates reading multiple values into a slice.
package main import "fmt" func main() { var numbers = make([]int, 3) fmt.Print("Enter three numbers: ") fmt.Scanln(&numbers[0], &numbers[1], &numbers[2]) sum := numbers[0] + numbers[1] + numbers[2] fmt.Printf("Sum: %d\n", sum) }
Each slice element must be addressed individually. The function reads values directly into the slice's elements.
Advanced error handling
For robust input handling, we can combine fmt.Scanln
with
other techniques. This example shows a more complete solution.
package main import ( "bufio" "fmt" "os" "strconv" "strings" ) func main() { reader := bufio.NewReader(os.Stdin) fmt.Print("Enter two numbers: ") input, _ := reader.ReadString('\n') input = strings.TrimSpace(input) parts := strings.Split(input, " ") if len(parts) != 2 { fmt.Println("Please enter exactly two numbers") return } num1, err1 := strconv.Atoi(parts[0]) num2, err2 := strconv.Atoi(parts[1]) if err1 != nil || err2 != nil { fmt.Println("Please enter valid numbers") return } fmt.Printf("Sum: %d\n", num1+num2) }
This approach provides more control over input parsing and error handling.
It's useful when fmt.Scanln
's behavior is too restrictive.
Source
This tutorial covered the fmt.Scanln
function in Go with practical
examples of reading and processing user input.
Author
List all Golang tutorials.