Go Formatting Verbs
fmt.Printf and fmt.Sprintf replace verbs — sequences starting with % — with the values you pass. Choosing the right verb lets you control exactly how numbers, text and data structures look.
General verbs
These work with any value:
| Verb | Prints |
|---|---|
%v |
the value in its default format |
%+v |
like %v, but structs include field names |
%#v |
Go-syntax representation (as you would type it in code) |
%T |
the type of the value |
%% |
a literal % |
package main
import "fmt"
type Point struct {
X, Y int
}
func main() {
p := Point{3, 4}
nums := []int{1, 2}
fmt.Printf("%v\n", p)
fmt.Printf("%+v\n", p)
fmt.Printf("%#v\n", p)
fmt.Printf("%T\n", p)
fmt.Printf("%v %#v %T\n", nums, nums, nums)
fmt.Printf("Progress: 75%%\n")
}{3 4}
{X:3 Y:4}
main.Point{X:3, Y:4}
main.Point
[1 2] []int{1, 2} []int
Progress: 75%
Tip: When debugging,
%+vand%#vare your friends — they show field names and exact types.
Integer verbs
| Verb | Meaning | 42 prints as |
|---|---|---|
%d |
base 10 | 42 |
%b |
base 2 | 101010 |
%o |
base 8 | 52 |
%x / %X |
base 16, lower/upper case | 2a / 2A |
%c |
the character with that code point | * |
%q |
quoted character | '*' |
%U |
Unicode notation | U+002A |
package main
import "fmt"
func main() {
n := 255
fmt.Printf("%d %b %o %x %X\n", n, n, n, n, n)
fmt.Printf("%#x %#o\n", n, n) // # adds the 0x / 0 prefix
fmt.Printf("%c %q %U\n", 71, 71, 71)
}255 11111111 377 ff FF
0xff 0377
G 'G' U+0047
Float verbs
| Verb | Meaning |
|---|---|
%f |
decimal point, 6 decimals by default |
%.2f |
exactly 2 decimals |
%e |
scientific notation |
%g |
the shortest of %e / %f |
package main
import "fmt"
func main() {
x := 1234.56789
fmt.Printf("%f\n", x)
fmt.Printf("%.2f\n", x)
fmt.Printf("%.0f\n", x)
fmt.Printf("%e\n", x)
fmt.Printf("%g\n", x)
}1234.567890
1234.57
1235
1.234568e+03
1234.56789
String verbs
| Verb | Meaning |
|---|---|
%s |
plain string |
%q |
double-quoted, with escapes |
%x |
hex dump of the bytes |
Width, padding and alignment
A number between % and the verb sets the minimum width. Text is right-aligned by default; the - flag left-aligns it, and 0 pads numbers with zeros. This is perfect for printing tables.
package main
import "fmt"
func main() {
items := []string{"Tea", "Croissant", "Juice"}
prices := []float64{2.5, 3.75, 4}
fmt.Printf("|%-10s|%8s|\n", "Item", "Price")
for i, item := range items {
fmt.Printf("|%-10s|%8.2f|\n", item, prices[i])
}
fmt.Printf("[%5d] [%-5d] [%05d]\n", 42, 42, 42)
fmt.Printf("%q\n", "say \"hi\"")
}|Item | Price|
|Tea | 2.50|
|Croissant | 3.75|
|Juice | 4.00|
[ 42] [42 ] [00042]
"say \"hi\""
| Flag | Effect | Example |
|---|---|---|
- |
left-align | %-8s |
0 |
pad with zeros | %05d |
+ |
always print the sign | %+d → +7 |
# |
alternate format | %#x → 0x1f |
(space) |
leave a space for the sign | % d → 7 |
Wrong verbs don't crash
If a verb doesn't match its argument, or the number of arguments is wrong, Printf still prints — but marks the problem in the output. go vet catches these mistakes before you run the program.
package main
import "fmt"
func main() {
fmt.Printf("%d\n", "text")
fmt.Printf("%s and %s\n", "one")
}%!d(string=text)
one and %!s(MISSING)
Test yourself
Which verb prints the type of a value?
%v%t%T%y
What does fmt.Printf("%05d", 42) print?
42000420004242
Which verb shows struct field names, like {X:3 Y:4}?
%v%+v%s%d