ZetCode

Go Scan

last modified June 6, 2022

Go scan tutorial shows how to read standard input in Golang with scan functions. The scan functions are located in the fmt package.

Scan functions

There are three scan functions: scan, scanln, and scanf.

func Scan(a ...any) (n int, err error)

The scan function scans text read from standard input and stores successive space-separated values into given arguments. A newline character counts as space. This means that we can continue writing input arguments after we pressed Enter.

func Scanln(a ...any) (n int, err error)

The Scanln is similar to Scan, but stops scanning at a newline. Ffter the final item there must be a newline or EOF.

func Scanf(format string, a ...any) (n int, err error)

The Scanf function reads from standard input and storing successive space-separated values into the given arguments, as determined by the string format. It returns the number of items successfully scanned. The function allows more sophisticated scanning.

Go Scan

In the first example, we use the Scan function.

main.go
package main

import (
    "fmt"
    "log"
)

func main() {

    var name string

    fmt.Print("Enter your name: ")

    _, err := fmt.Scan(&name)

    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Hello %s!\n", name)
}

The example reads an input from a user, builds a message and prints it to the console.

$ go run main.go
Enter your name: Peter
Hello Peter!

main.go
package main

import (
    "fmt"
    "log"
)

func main() {
    var name string
    var age int

    fmt.Print("Enter your name and age: ")

    n, err := fmt.Scan(&name, &age)

    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("scanned %d arguments\n", n)
    fmt.Printf("%s is %d years old\n", name, age)
}

We scan two arguments. We print the number of scanned arguments and a message built from these two arguments.

$ go run main.go
Enter your name and age: Peter 23
scanned 2 arguments
Peter is 23 years old

Go Scanln

With Scanln, the newline character terminates the scanning process.

main.go
package main

import (
    "fmt"
    "log"
)

func main() {

    var name1 string
    var name2 string
    var name3 string

    fmt.Print("Enter three names: ")

    _, err := fmt.Scanln(&name1, &name2, &name3)

    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Entered names: %s, %s, %s\n", name1, name2, name3)
}

We use the Scanln function to read three names.

Go Scanf

The Scanf function enables more advanced scanning.

main.go
package main

import (
    "fmt"
    "log"
)

func main() {

    var n1, n2, n3, n4, n5 int

    fmt.Print("Enter five numbers separated by comma and space: ")

    _, err := fmt.Scanf("%d, %d, %d, %d, %d", &n1, &n2, &n3, &n4, &n5)

    if err != nil {
        log.Fatal(err)
    }

    sum := n1 + n2 + n3 + n4 + n5

    fmt.Printf("The sum of five numbers is: %d\n", sum)
}

In this example, we read five numbers. The numbers have to be separated by a comma and a space character.

$ go run main.go
Enter five numbers separated by comma and space: 1, 2, 3, 4, 5
The sum of five numbers is: 15

In this tutorial, we have worked with the scan functions in Golang.

List all Go tutorials.