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)

Print functions

FunctionDescription
Print(a ...any) (n int, err error)
Writes operands to standard output, adding spaces between operands when neither is a string.
Println(a ...any) (n int, err error)
Println("go", 1) // "go 1\n"
Like Print, but always spaces operands and appends a trailing newline.
Printf(format string, a ...any) (n int, err error)
Printf("%s=%d\n", "x", 1)
Writes a format string to standard output, substituting a verb for each operand in order.
Sprint(a ...any) string
Like Print, but returns the formatted result as a string instead of writing it.
Sprintln(a ...any) string
Like Println, but returns the formatted result as a string instead of writing it.
Sprintf(format string, a ...any) string
Sprintf("%02d:%02d", 9, 5) // "09:05"
Like Printf, but returns the formatted result as a string instead of writing it.
Fprint(w io.Writer, a ...any) (n int, err error)
Like Print, but writes to any io.Writer instead of standard output.
Fprintln(w io.Writer, a ...any) (n int, err error)
Like Println, but writes to any io.Writer instead of standard output.
Fprintf(w io.Writer, format string, a ...any) (n int, err error)
Fprintf(os.Stderr, "warn: %v\n", err)
Like Printf, but writes to any io.Writer instead of standard output.
Append(b []byte, a ...any) []byte
Like Print, but appends the formatted result to b instead of writing it.
Appendln(b []byte, a ...any) []byte
Like Println, but appends the formatted result to b instead of writing it.
Appendf(b []byte, format string, a ...any) []byte
Like Printf, but appends the formatted result to b instead of writing it.
Errorf(format string, a ...any) error
Errorf("open %s: %w", path, err)
Formats like Sprintf, but returns an error. A %w verb wraps another error instead of just stringifying it.

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

Format Verb Notes Every verb, flag, width and precision form, with the exact output each one prints. Plus the interfaces that change it, and which verb to reach for.