HowtoGo
Home / Standard Library / The fmt Package
Standard Library

The fmt Package

fmt formats values for printing, building strings, and reading input. Every %-verb, and the Print/Scan functions that consume them, live here.

Examples

Printf formats according to a format specifier and writes to standard output. Use with format specifiers like %T - which prints a value's Type.

name := "Gopher"
age := 5
price := 19.99
active := true

fmt.Printf("%s is %d years old\n", name, age)
fmt.Printf("price: %.2f, active: %t\n", price, active)
fmt.Printf("%q\n", name)
fmt.Printf("%v (%T)\n", price, price)
Output
Gopher is 5 years old
price: 19.99, active: true
"Gopher"
19.99 (float64)

Scan functions

FunctionDescription
Scan(a ...any) (n int, err error)
Reads space-separated values from standard input into the pointers in a, newlines count as spaces.
Scanln(a ...any) (n int, err error)
Like Scan, but stops at a newline and requires one to appear after the last item.
Scanf(format string, a ...any) (n int, err error)
Scanf("%d-%d", &m, &d)
Reads from standard input according to a format string, mirroring Printf's verbs.
Sscan(str string, a ...any) (n int, err error)
Like Scan, but reads from the string str instead of standard input.
Sscanln(str string, a ...any) (n int, err error)
Like Scanln, but reads from the string str instead of standard input.
Sscanf(str, format string, a ...any) (n int, err error)
Sscanf("09:05", "%d:%d", &h, &m)
Like Scanf, but reads from the string str instead of standard input.
Fscan(r io.Reader, a ...any) (n int, err error)
Like Scan, but reads from any io.Reader instead of standard input.
Fscanln(r io.Reader, a ...any) (n int, err error)
Like Scanln, but reads from any io.Reader instead of standard input.
Fscanf(r io.Reader, format string, a ...any) (n int, err error)
Like Scanf, but reads from any io.Reader instead of standard input.

Verbs