What is the Scanln function in Golang?

The Scanln function in the Go programming language is used to read the standard input data and store resulting strings into the destinations specified by a list of additional arguments you provide. This function differs from the simple Scan function because it stops the scanning process when it encounters an EOF or newline character.

To use this function, you must import the fmt package in your file and access the Scanln function within it using the . notation (fmt.Scanln). Here, Scanln is the actual function, while fmt is the Go package that stores the definition of this function.

Function definition

The definition of the Scanln function inside the fmt package is:

Parameters

fmt.Scanln takes a writing destination along with a list of a variable number of arguments.

  • a ...interface{}: The list of all arguments that you want to store data in. After taking in the input, the input string is automatically split on space characters. The components are then stored sequentially into the given arguments. If there are fewer arguments than the different splits of the input string, the extra pieces are discarded.

Return values

The fmt.Scanln function can return two things:

  • count: The number of arguments the function writes to.

  • err: Any error thrown during the execution of the function.

Example

In the following example, we use the Scanln function to take input from standard input and store the data for several other variables in it.

Scanln reads from the input source sequentially. Hence, we must give the list of arguments in the order specified in the format string.

Here, we require an input with the specific format matching the sequence of arguments we have given the Scanln function.

For example, with the first word being a string, the second can be anything since it is stored in temp and is unused. The third can be an int, and another string after that.

An example input would be “Faraz owns 500 acres of land”. We then use the Scanln function to read this input and store parts of the string that corresponds to the amount of land, units of measurement, and the owner’s name. Next, we use Printf to print a new string.

Enter input, then press RUN.

package main
import (
"fmt"
)
func main() {
// the message we try here would be:
// Faraz owns 500 acres of land
// or something follwoing the same format
var name string
var unit string
var amount int
var temp string
// taking input and storing in variable using the buffer string
fmt.Scanln(&name, &temp, &amount, &unit)
// print out new string using the extracted values
fmt.Printf ("%d %s of land is owned by %s\n",amount, unit, name);
}

Enter the input below

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved