0Pricing
Go Academy · Lesson

Scanning Input

Read formatted input.

Scanning Input is a free Go Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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)
}

Sscan Splits on Spaces

fmt.Sscan reads space-separated values from a string into multiple variables, picking the type from each pointer.

package main

import "fmt"

func main() {
    var name string
    var age int
    fmt.Sscan("Ada 36", &name, &age)
    fmt.Println(name, age)
}

Sscanf Uses a Format

fmt.Sscanf parses according to a format string, just like Printf in reverse. Literal characters in the format must match the input.

package main

import "fmt"

func main() {
    var h, m int
    fmt.Sscanf("09:05", "%d:%d", &h, &m)
    fmt.Println(h, m)
}

Checking How Many Were Read

Scan functions return the count of successfully parsed items and an error. Check the count to know if parsing fully succeeded.

package main

import "fmt"

func main() {
    var a, b int
    count, _ := fmt.Sscan("7", &a, &b)
    fmt.Println("parsed:", count)
}

Handling Parse Errors

If the input does not match the expected type, an error is returned. Always inspect it for real input.

package main

import "fmt"

func main() {
    var n int
    _, err := fmt.Sscan("notanumber", &n)
    if err != nil {
        fmt.Println("error:", err)
    }
}

Parsing Mixed Types

You can read several different types in one call. Each pointer's type decides how its token is parsed.

package main

import "fmt"

func main() {
    var word string
    var price float64
    var ok bool
    fmt.Sscan("apple 1.50 true", &word, &price, &ok)
    fmt.Println(word, price, ok)
}

Sscanf with Literal Text

Sscanf is great for fixed shapes like coordinates or key-value pairs, where punctuation separates the numbers.

package main

import "fmt"

func main() {
    var x, y int
    fmt.Sscanf("(3,8)", "(%d,%d)", &x, &y)
    fmt.Println(x, y)
}

Reading Floats

Use %f in Sscanf or a *float64 in Sscan to read decimal numbers.

package main

import "fmt"

func main() {
    var temp float64
    fmt.Sscanf("21.5C", "%fC", &temp)
    fmt.Println(temp)
}

Scan vs Stdin

fmt.Scan and fmt.Scanf read from standard input the same way Sscan reads from a string. The parsing rules are identical; only the source differs.

package main

import "fmt"

func main() {
    line := "100 200"
    var a, b int
    fmt.Sscan(line, &a, &b)
    fmt.Println(a * b)
}

A Small Parser

Combine scanning with a calculation to parse a record and produce a result.

package main

import "fmt"

func main() {
    var name string
    var qty int
    var unit float64
    fmt.Sscanf("pen 3 1.25", "%s %d %f", &name, &qty, &unit)
    fmt.Printf("%s total: $%.2f\n", name, float64(qty)*unit)
}

Quick Check

What must you pass to a scan function to store the result?

Recap: Scanning Input

You learned to read formatted input:

  • Sscan splits on whitespace; Sscanf follows a format string.
  • Pass pointers with & for each target.
  • Check the returned count and error to detect bad input.
package main

import "fmt"

func main() {
    var d, mo, y int
    fmt.Sscanf("30/05/2026", "%d/%d/%d", &d, &mo, &y)
    fmt.Println(y, mo, d)
}

Frequently asked questions

Is the “Scanning Input” lesson free?

Yes — the full text of “Scanning Input” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Scanning Input”?

Read formatted input. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Scanning Input” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

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