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).
fmt.Printf("%v %+v %#v %T\n", p, p, p, p)
Try it
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.
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
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 vetchecks format strings at build time and reports most of these mistakes.
Usage notes
%vis the default.Printlnuses the same formatting as%vfor every operand.- Methods change
%vand%s. If a value has anError() stringorString() stringmethod,%v,%sand%qcall it.%dand%xon a type that has aStringmethod still print the underlying number. %+vis the best debug verb for structs, and%#vis best when you need to distinguish[]string{}from[]string(nil).- Runes are integers.
%don aruneprints the code point; use%cfor the character and%qfor a quoted character. - Strings vs bytes.
%sworks onstring,[]byteand anything withString().%xon a string gives its UTF-8 bytes, not code points. %wis only forfmt.Errorf(). In other functions it prints as an error marker.- Floats:
%von afloat64uses%g, so100000000.0prints as1e+08. Use%for%.2ffor fixed notation. - Argument indexes
%[2]d %[1]dreorder or repeat arguments.