Tuesday, February 10, 2015

fmt.Printf format reference

Here's the fmt.Printf format reference for Go (golang):

General

the value in a default format%v
type of the value%T

Integer

+255Always show sign%+d
'ÿ'Single quoted%q
377Octal without leading ‘0’%o
0377Octal with leading ‘0’%#o
ffHex lowercase%x
FFHex uppercase%X
0xffHex with “0x”%#x
U+00FFUnicode%U
U+00FF ‘ÿ’Unicode with char%#U
11111111Binary%b

Integer width

|    1|
|   12|
|  123|
| 1234|
|12345|
|123456|
|1234567|
|1    |
|12   |
|123  |
|1234 |
|12345|
|123456|
|1234567|
Minimum width=5. Expand if wider.%5d
%-5d (Left justify)
|    1|
|   12|
|  123|
| 1234|
|12345|
|12345|
|12345|
|1    |
|12   |
|123  |
|1234 |
|12345|
|12345|
|12345|
Width=5. Truncate right if wider.fmt.Printf(“%s”, fmt.Sprintf(“%5d”, value)[:5]))
fmt.Printf(“%s”, fmt.Sprintf(“%-5d”, value)[:5])) (Left justify)
  or
fmt.Printf(“|%5.5s|\n”, strconv.Itoa(value))
fmt.Printf(“|%-5.5s|\n”, strconv.Itoa(value)) (Left justify)
|    1|
|   12|
|  123|
| 1234|
|12345|
|23456|
|34567|
|1    |
|12   |
|123  |
|1234 |
|12345|
|23456|
|34567|
Width=5. Truncate left if wider.fmt.Printf(“%5d”, val % 100000)
fmt.Printf(“%-5d”, val % 100000) (Left justify)
|0012345|Pad with ‘0’ (Right justify only)%07d

Float

original%f (=%.6f)
6 digits after decimal
%e (=%.6e)
6 digits after decimal
%g
smallest number of digits necessary
%.3g
total number of digits=3
12345
1234.5
123.45
12.345
1.2345
0.12345
0.012345
0.0012345
0.00012345
0.000012345
0.0000012345
12345.000000
1234.500000
123.450000
12.345000
1.234500
0.123450
0.012345
0.001234
0.000123
0.000012
0.000001
1.234500e+04
1.234500e+03
1.234500e+02
1.234500e+01
1.234500e+00
1.234500e-01
1.234500e-02
1.234500e-03
1.234500e-04
1.234500e-05
1.234500e-06
12345
1234.5
123.45
12.345
1.2345
0.12345
0.012345
0.0012345
0.00012345
1.2345e-05
1.2345e-06
1.23e+04
1.23e+03
123
12.3
1.23
0.123
0.0123
0.00123
0.000123
1.23e-05
1.23e-06

String

love is "愛"Normal%s
"love is \"愛\""Double-quoted%q
`love is "愛"`Back-quoted if possible. Otherwise, double-quoted%#q
"love is \"\u611b\""Double-quoted. ASCII only.%+q
6c6f76652069732022e6849b22Hex lowercase.%x
6C6F76652069732022E6849B22Hex uppercase.%X
6c 6f 76 65 20 69 73 20 22 e6 84 9b 22Hex with space.% x

String width

|    1|
|   12|
|  123|
| 1234|
|12345|
|123456|
|1234567|
|12345678|
|123456789|
|1234567890|
|1    |
|12   |
|123  |
|1234 |
|12345|
|123456|
|1234567|
|12345678|
|123456789|
|1234567890|
Minimum width=5. Expand if wider.%5s
%-5s (Left justify)
|1|
|12|
|123|
|1234|
|12345|
|12345|
|12345|
Maximum width=5(Expand up to 5).%.5s
|    1|
|   12|
|  123|
| 1234|
|12345|
|123456|
|1234567|
|1234567|
|1234567|
|1234567|
|1    |
|12   |
|123  |
|1234 |
|12345|
|123456|
|1234567|
|1234567|
|1234567|
|1234567|
Minimum width=5. Expand up to 7.%5.7s
%-5.7 (Left justify)
|    1|
|   12|
|  123|
| 1234|
|12345|
|12345|
|12345|
|1    |
|12   |
|123  |
|1234 |
|12345|
|12345|
|12345|
Width=5.%5.5s
%-5.5s (Left justify)
|    1|
|   12|
|  123|
|  123|
|  123|
|1    |
|12   |
|123  |
|123  |
|123  |
Width=5. Truncate if wider than 3.%5.3s
%-5.3s (Left justify)
|0012345||12345  |Pad with ‘0’. Doen’t work when left-justify%07s

Struct

{sam {12345 67890}}Normal%v
{name:sam phone:{mobile:12345 office:67890}With field names%+v
main.People{name:”sam”, phone:main.Phone{mobile:”12345”, office:”67890”}}Go’s syntax%#v

Boolean

true
false
Boolean%t

Pointer

0x1842c220Pointer with 0x%p
1842c220Pointer without 0x%#p

References

http://golang-examples.tumblr.com/post/86795367134/fmt-printf-format-reference-cheat-sheet


Share this article


This work is licensed under the Creative Commons Attribution 3.0 Unported License.

1 comment: