Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท fmt Package

Go fmt verbs

Reference for the Go fmt formatting verbs: general verbs like %v and %T, integer, float, string, pointer and boolean verbs, and what happens with a wrong verb.

A verb is the part of a format string that starts with % and tells fmt how to render one argument. The Printf, Sprintf, Fprintf and Errorf functions all read the same verb language, and Sscanf and friends use a similar one for input.

Verbs fall into a few families: general verbs that work on any value (%v, %T), verbs for integers, floats, strings and booleans, and verbs for pointers. Pick the verb for the kind of value you have, and add width, precision or flags in between when you need alignment (see Width, precision and flags).

Go
fmt.Printf("%v %+v %#v %T\n", p, p, p, p)

Try it

Go General verbs on a struct

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Integer, float and string verbs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
func Printf(format string, a ...any) (n int, err error)

General verbs (any value):

Verb Description
%v The default format for the value. Structs print as {3 4}, slices as [a b], maps as map[k:v], pointers to structs as &{3 4}.
%+v Like %v, but struct fields are printed with their names.
%#v A Go-syntax representation that could be pasted into source (main.Point{X:3, Y:4}).
%T The Go type of the value, such as main.Point or []string.
%% A literal percent sign. It consumes no argument.

Integers: %d decimal, %b binary, %o octal, %O octal with 0o prefix, %x / %X hex (lower/upper case), %c the character for a code point, %q a single-quoted character literal, %U Unicode format (U+1F600).

Floats and complex numbers: %f (also %F) decimal without exponent, %e / %E scientific notation, %g / %G the shorter of the two, %b decimal-less exponent form, %x hexadecimal float.

Strings and byte slices: %s the text, %q a double-quoted, escaped string, %x / %X the bytes as hex (a space flag separates bytes).

Booleans: %t prints true or false.

Pointers: %p prints the address in hex. Addresses differ from run to run, so avoid them in tests and examples that need fixed output.

Errors and edge cases:

  • A verb that does not fit the value does not crash. It prints a marker such as %!d(string=oops).
  • Too few arguments prints %!d(MISSING); too many appends %!(EXTRA int=3).
  • An unknown verb prints %!z(int=1).
  • go vet checks format strings at build time and reports most of these mistakes.

Usage notes

  • %v is the default. Println uses the same formatting as %v for every operand.
  • Methods change %v and %s. If a value has an Error() string or String() string method, %v, %s and %q call it. %d and %x on a type that has a String method still print the underlying number.
  • %+v is the best debug verb for structs, and %#v is best when you need to distinguish []string{} from []string(nil).
  • Runes are integers. %d on a rune prints the code point; use %c for the character and %q for a quoted character.
  • Strings vs bytes. %s works on string, []byte and anything with String(). %x on a string gives its UTF-8 bytes, not code points.
  • %w is only for fmt.Errorf(). In other functions it prints as an error marker.
  • Floats: %v on a float64 uses %g, so 100000000.0 prints as 1e+08. Use %f or %.2f for fixed notation.
  • Argument indexes %[2]d %[1]d reorder or repeat arguments.