*printf series functions support format formatting parameters. Here, we categorize according to the data type of the variables replaced in the placeholders for easy lookup and memorization.
Placeholder List
| Category | Placeholder | Description |
|---|---|---|
| General Placeholder | %v | Default format representation of the value |
%+v | Similar to %v, but adds field names when printing structs | |
%#v | Go-syntax representation of the value | |
%T | Print the type of the value | |
%% | Percent sign | |
| Boolean | %t | true or false |
| Integer | %b | Represented in binary |
%c | The Unicode code point for the value | |
%d | Represented in decimal | |
%o | Represented in octal | |
%x | Represented in hexadecimal (using a-f) | |
%X | Represented in hexadecimal (using A-F) | |
%U | Represented in Unicode format: U+1234, equivalent to U+%04X | |
%q | Quoted Go-syntax character literal of the value, with possible escapes for safety | |
| Floating Point and Complex | %b | Scientific notation with binary exponent, e.g.: -123456p-78 |
%e | Scientific notation, e.g.: -1234.456e+78 | |
%E | Scientific notation, e.g.: -1234.456E+78 | |
%f | Decimal point but no exponent, e.g.: 123.456 | |
%F | Equivalent to %f | |
%g | Uses %e or %f format depending on the situation for more concise and accurate output | |
%G | Uses %E or %F format depending on the situation for more concise and accurate output | |
| String and []byte | %s | Directly outputs the string or []byte |
%q | Double-quoted Go-syntax string literal, with possible escapes for safety | |
%x | Represented in two-character hexadecimal for each byte (using a-f) | |
%X | Represented in two-character hexadecimal for each byte (using A-F) | |
| Pointer | %p | Hexadecimal representation with 0x prefix |
| Width Specifier | %f | Default width, default precision |
%9f | Width 9, default precision | |
%.2f | Default width, precision 2 | |
%9.2f | Width 9, precision 2 | |
%9.f | Width 9, precision 0 | |
| Placeholder Modifiers | + | Always show sign for numeric values; for %q (%+q) generates ASCII-only output (with escapes) |
| Space, prefixes positive numbers with a space and negative numbers with a sign; for %x or %X (% x or % X), inserts spaces between printed bytes | |
- | Pads the right instead of the default left (right-align to left-align switch) | |
# | Prefixes octal with 0 (%#o), hexadecimal with 0x (%#x) or 0X (%#X), strips 0x for pointers (%#p); for %q (%#q) and %U (%#U), outputs Go literal with quotes and spaces | |
0 | Pads with 0 instead of spaces, placed after sign for numeric types | |