GoLang fmt Printing Cheat Sheet
by Guillermo Pascual (gpascual) via [Link]/32181/cs/9868/
String and slice of bytes Boolean
%s the uninterpreted bytes of the string or slice %t the word true or false
%q a double-quoted string safely escaped with Go syntax
Integer
%x base 16, lower-case, two characters per byte
%X base 16, upper-case, two characters per byte %b base 2
%c the character represented by the corresponding Unicode code point
General %d base 10
%v The value in a default format. When printing structs, the plus flag %o base 8
(%+v) adds field names.
%q a single-quoted character literal safely escaped with Go syntax
%#v a Go-syntax representation of the value
%x base 16, with lower-case letters for a-f
%T a Go-syntax representation of the type of the value %X base 16, with upper-case letters for A-F
%% a literal percent sign; consumes no value
%U Unicode format: U+1234; same as "U+%04X"
The default format for %v Floating-point and complex constituents
bool: %t %b decimalless scientific notation with exponent a power of two, in the
int, int8 etc.: %d manner of strconv.FormatFloat with the 'b' format, e.g. -123456p-78
uint, uint8 etc.: %d, %x if printed with %#v %e scientific notation, e.g. -1.234456e+78
float32, complex64, etc: %g %E scientific notation, e.g. -1.234456E+78
string: %s %f decimal point but no exponent, e.g. 123.456
chan: %p %F synonym for %f
pointer: %p %g %e for large exponents, %f otherwise
%G %E for large exponents, %F otherwise
Other flags
+ always print a sign for numeric values; guarantee ASCII-only Floating-point Precision
output for %q (%+q). %f default width, default precision
- pad with spaces on the right rather than the left (left-justify the
%9f width 9, default precision
field).
%.2f default width, precision 2
# alternate format: add leading 0 for octal (%#o), 0x for hex
%9.2f width 9, precision 2
(%#x); 0X for hex (%#X); suppress 0x for %p (%#p); for %q,
print a raw (backquoted) string if strconv.CanBackquote returns %9.f width 9, precision 0
true;
'' leave a space for elided sign in numbers (% d); put spaces Pointer
(space) between bytes printing strings or slices in hex (% x, % X).
%p base 16 notation, with leading 0x
0 pad with leading zeros rather than spaces; for numbers, this
moves the padding after the sign.
By Guillermo Pascual Published 17th November, 2016. Sponsored by [Link]
(gpascual) Last updated 17th November, 2016. Measure your website readability!
[Link]/gpascual/ Page 1 of 1. [Link]