What is the Scan function in Golang?

The Scan function in the Go programming language is used to read data from the standard input, format the string, and store the resultant strings into the destinations specified by the additional arguments.

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

Function definition

The definition of the Scan function inside the fmt package is as follows:

Parameters

This function takes in a single argument: a ...interface{}, which is the list of all arguments that you want your inputs to be stored in.

After taking in the input, the input string is automatically split on space characters, and the components are stored sequentially into the given arguments. If there are fewer arguments than the different input string splits, then the extra pieces are discarded.

Return values

The fmt.Scan function can return two things:

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

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

Examples

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

Scan reads from the input string 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 Scan 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 Scan function to read this input and store parts of the string corresponding to the amount of land, units of measurement, and the owner’s name. We then use Printf to print a new string.

Enter input, then press RUN.

package main
import (
"fmt"
)
func main() {
var name string
var unit string
var amount int
var temp string
// taking input and storing in variable using a sample input string would be:
// "Faraz owns 500 acres of land"
fmt.Scan(&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

The following example is exactly the same as the one above, but it shows how you can make use of the return values of the Scan function.

Enter input, then press RUN.

package main
import (
"fmt"
)
func main() {
var name string
var unit string
var amount int
var temp string
// taking input and storing in variable using a sample input string would be:
// "Faraz owns 500 acres of land"
char_count, error := fmt.Scan(&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);
if error == nil {
fmt.Print("Scan executed without errors and wrote ",char_count, " arguments")
}
}

Enter the input below

Free Resources

HowDev By Educative. Copyright ©2025 Educative, Inc. All rights reserved