How to format anything (string, number or arbitrary data) with fmt


With the fmt package you can format numbers and strings padded with spaces or zeroes, in different bases, and with optional quotes.

You submit a template string that contains the text you want to format plus some annotation verbs that tell the fmt functions how to format the trailing arguments.

In this example

fmt.Printf("Binary: %b\\%b", 4, 5)
// Binary: 100\101 

Use the special verb %%, which consumes no argument, to write a literal percent sign.

fmt.Printf("%d %%", 50) // "50 %"

Format a string without printing it

Use the fmt.Sprintf function to format a string without printing it.

s := fmt.Sprintf("Binary: %b\\%b", 4, 5) // s == "Binary: 100\101"

Default formats and type: []int64{0, 1}

Formatting Description Verb
[0 1] Default format %v
[]int64{0, 1} Go-syntax format %#v
[]int64 The type of the value %T

Integer (base, padding, sign): 15

Formatting Description Verb
15 Base 10 %d
+15 Always show sign %+d
␣␣15 Pad with space (width 4, right justified) %4d
15␣␣ Pad with space (width 4, left justified) %-4d
0015 Pad with zero (width 4) %04d
1111 Base 2 %b
17 Base 8 %o
f Base 16, lowercase %x
F Base 16, uppercase %X
0xf Base 16, with leading 0x %#x

Character (quoted, Unicode): 65

Formatting Description Verb
A Character %c
'A' Quoted character %q
U+0041 Unicode %U
U+0041 'A' Unicode with character %#U

Boolean (true/false)

Use %t to format a boolean as true or false.

Pointer (hex)

Use %p to format a pointer in base 16 notation with leading 0x.

Float (padding, precision, scientific notation): 123.456

Formatting Description Verb
1.234560e+02 Scientific notation %e
123.456000 Decimal point, no exponent %f
123.46 Default width, precision 2 %.2f
␣␣123.46 Width 8, precision 2 %8.2f
123.456 Exponent as needed, necessary digits only %g

String or byte slice (quoted, padding, hex): "café"

Formatting Description Verb
café Plain string %s
␣␣café Width 6, right justify %6s
café␣␣ Width 6, left justify %-6s
"café" Quoted string %q
636166c3a9 Hex dump of byte values %x
63 61 66 c3 a9 Hex dump with spaces % x

Special values

Value Description
\a U+0007 alert or bell
\b U+0008 backspace
\\ U+005c backslash
\t U+0009 horizontal tab
\n U+000A line feed or newline
\f U+000C form feed
\r U+000D carriage return
\v U+000b vertical tab

Arbitrary values can be encoded with backslash escapes and can be used in any "" string literal.

There are four different formats:

The escapes \u and \U represent Unicode code points.

// Output:
// \café

Find incorrect formatting with vet

If you compile and run this line of code

fmt.Printf("Binary: %b\\%b", 4)

the program will print

Binary: 100\%!b(MISSING)

To catch these kinds of bugs, you can use the vet command – it can find calls whose arguments do not align with the format string.

$ go vet example.go
example.go:8: missing argument for Printf("%b"): format reads arg 2, have only 1 args

Further reading

See String functions cheat sheet for more string formatting functions.

Share this page: