Go Formatting Verbs
In Go, "formatting verbs" are special placeholders used with functions like fmt.Printf to control how values are displayed.
Common Verbs
| Verb | Usage | Example Output |
|---|---|---|
%v |
Default format (works for almost anything) | {val: 123} |
%+v |
Struct with field names | {Name: "John"} |
%T |
The Type of the value | int, string |
%d |
Decimal integer | 15 |
%b |
Binary integer | 1111 |
%f |
Float (decimal) | 1.234567 |
%.2f |
Float with 2 decimal places | 1.23 |
%s |
Basic string | hello |
%q |
Quoted string | "hello" |
%p |
Pointer (memory address) | 0xc000... |
Basic Example
Width and Padding
You can also control the spacing of your output:
%5d: Puts at least 5 spaces of width, right-justified.%-5d: Puts at least 5 spaces of width, left-justified.%05d: Pads with zeros (e.g.,00042).