0Pricing
Go Academy · Lesson

Scanning Input

Read formatted input.

Reading Input with fmt

The fmt package can read values, not just print them. The scan functions parse text into typed variables.

They are the mirror image of the print functions.

package main

import "fmt"

func main() {
    var n int
    fmt.Sscan("42", &n)
    fmt.Println(n + 1)
}

Pointers Are Required

Scan functions must store a result, so you pass the address of each variable using &.

Forgetting & is the most common scanning bug.

package main

import "fmt"

func main() {
    var a, b int
    fmt.Sscan("3 4", &a, &b)
    fmt.Println(a + b)
}

All lessons in this course

  1. Printf Verbs
  2. Sprintf and Fprintf
  3. The Stringer Interface
  4. Scanning Input
← Back to Go Academy