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)Gopher is 5 years old
price: 19.99, active: true
"Gopher"
19.99 (float64)Sprintf runs the same formatting engine as Printf but returns a string instead of writing it, for building a value that's stored or passed on rather than printed directly.
userID := 42
key := fmt.Sprintf("user:%d:sessions", userID)
fmt.Println(key)user:42:sessionsFprintf writes to any io.Writer: standard error, a bytes.Buffer, an open file, an HTTP response. Printf is just Fprintf(os.Stdout, ...) underneath.
var buf bytes.Buffer
count := 7
fmt.Fprintf(&buf, "%d items", count)
fmt.Println(buf.String())
fmt.Fprintf(os.Stderr, "warning: %v\n", err)7 items
warning: lookup 42: not found%v is the default form. %+v adds field names for a struct. %#v prints Go syntax you could paste back into source.
type Point struct{ X, Y int }
p := Point{3, 4}
fmt.Printf("%v\n", p)
fmt.Printf("%+v\n", p)
fmt.Printf("%#v\n", p){3 4}
{X:3 Y:4}
main.Point{X:3, Y:4}A String() string method satisfies fmt.Stringer. Println and both %v and %+v call it automatically; only %#v bypasses it to show the raw Go syntax.
type Point struct{ X, Y int }
func (p Point) String() string {
return fmt.Sprintf("(%d, %d)", p.X, p.Y)
}
p := Point{3, 4}
fmt.Printf("%v\n", p)
fmt.Printf("%+v\n", p)
fmt.Printf("%#v\n", p)(3, 4)
(3, 4)
main.Point{X:3, Y:4}A minus flag left-aligns, a plain number sets a minimum field width, and .N on a float sets decimal precision. This is the combination behind most hand-rolled column output.
rows := []struct {
Name string
Price float64
}{
{"Widget", 3.5},
{"Gadget", 19.99},
}
for _, r := range rows {
fmt.Printf("%-10s %6.2f\n", r.Name, r.Price)
}Widget 3.50
Gadget 19.99The same integer through four bases: binary, octal, and lower/upper-case hexadecimal.
fmt.Printf("%b %o %x %X\n", 255, 255, 255, 255)11111111 377 ff FF%q escapes a string into a double-quoted Go literal, or a rune into a single-quoted one, safe to print without breaking on control characters.
fmt.Printf("%q\n", "hi\n")
fmt.Printf("%q\n", 'A')"hi\n"
'A'Sscanf mirrors Printf's verbs in reverse, parsing a string into the pointers given after the format.
var h, m int
fmt.Sscanf("09:05", "%d:%d", &h, &m)
fmt.Println(h, m)9 5Print functions
| Function | Description |
|---|---|
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. |
Every verb in a format string must have a matching operand, in order. Too few operands prints a | |
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. |
Use | |
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. |
A single | |
Scan functions
| Function | Description |
|---|---|
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. |