The Go fmt package formats values with verbs — a percent sign followed by
a letter — that control how each argument is rendered. Choosing the wrong verb
gives misleading output or an explicit error placeholder. This tool is a
searchable reference of every common verb with its accepted types and a worked
example.
How it works
fmt.Printf, Sprintf, and friends scan the format string and pair each verb
with the next argument. Each verb has a default behaviour plus optional flags,
width, and precision that refine it. The reference groups verbs by purpose:
- General —
%v(default),%+v(with field names),%#v(Go syntax),%T(type),%%(literal percent). - Integers —
%d(decimal),%b(binary),%o(octal),%x/%X(hex),%c(rune),%U(Unicode). - Floats —
%f/%F,%e/%E(scientific),%g/%G(compact). - Strings/bytes —
%s,%q(quoted),%x(hex bytes). - Boolean —
%t. - Pointer —
%p.
Worked example
type User struct{ Name string; Age int }
u := User{"Ada", 36}
fmt.Printf("%v\n", u) // {Ada 36}
fmt.Printf("%+v\n", u) // {Name:Ada Age:36}
fmt.Printf("%#v\n", u) // main.User{Name:"Ada", Age:36}
fmt.Printf("%T\n", u) // main.User
fmt.Printf("%6.2f\n", 3.14159) // " 3.14"
fmt.Printf("%q\n", "hi\n") // "hi\n"
Notes
- Flags:
+always shows a sign,(space) leaves a space for the sign of positive numbers,-left-justifies,0pads with leading zeros,#enables the alternate form (0xprefix for%#x, etc.). - Width and precision can be supplied dynamically with
*, taking the value from the argument list, as infmt.Printf("%*d", 5, n). - A type mismatch produces a visible placeholder such as
%!d(string=hi)instead of a panic, so bugs surface in the output.