Go (Golang) – Formatting Verbs (fmt Package)
Formatting verbs are placeholders used with fmt.Printf(), fmt.Sprintf(), etc., to control how values are displayed.
1️⃣ General Formatting Verbs
| Verb |
Meaning |
Example |
%v |
Default format |
fmt.Printf("%v", x) |
%+v |
Struct with field names |
fmt.Printf("%+v", user) |
%#v |
Go-syntax representation |
fmt.Printf("%#v", user) |
%T |
Type of value |
fmt.Printf("%T", x) |
%% |
Print % |
fmt.Printf("%%") |
2️⃣ String Formatting Verbs
| Verb |
Meaning |
Example |
%s |
String |
"Hello" |
%q |
Quoted string |
"Hello" |
%x |
Hex (bytes) |
"go" → 676f |
3️⃣ Integer Formatting Verbs
| Verb |
Meaning |
%d |
Decimal |
%b |
Binary |
%o |
Octal |
%x |
Hex (lowercase) |
%X |
Hex (uppercase) |
%c |
Unicode character |
%U |
Unicode code point |
4️⃣ Floating-Point Formatting Verbs
| Verb |
Meaning |
%f |
Decimal point |
%.2f |
2 decimal places |
%e |
Scientific notation |
%E |
Scientific (uppercase) |
%g |
Compact format |
5️⃣ Boolean Formatting
| Verb |
Meaning |
%t |
Boolean (true / false) |
6️⃣ Pointer Formatting
| Verb |
Meaning |
%p |
Memory address |
7️⃣ Width & Precision
Width
Precision
Width + Precision
8️⃣ Alignment & Padding
Right-aligned (default)
Left-aligned
Zero padding
9️⃣ Formatting Structs & Maps
🔟 Common Mistakes
❌ Using wrong verb for type
✅ Correct
Summary Cheat Sheet
| Data Type |
Verb |
| Any |
%v |
| String |
%s |
| Int |
%d |
| Float |
%f |
| Bool |
%t |
| Type |
%T |
| Pointer |
%p |