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
- Printf Verbs
- Sprintf and Fprintf
- The Stringer Interface
- Scanning Input