Chapter 13: Formatting Verbs
Formatting Verbs.
These are also called format verbs, placeholders, or % verbs. They are the special codes starting with % inside the format string that tell Printf how to display each argument.
Go’s verbs are simpler than C’s printf, but very powerful — especially for structs, defaults, and debugging.
The official list comes straight from the fmt package documentation: https://pkg.go.dev/fmt → scroll to “Formatting” section (updated as of Go 1.26 in 2026).
I’ll teach this like your personal teacher: families of verbs, most useful ones first, flags/modifiers (width, precision, alignment), real examples, common patterns, and a big cheat-sheet table.
1. Quick Recap — Where Verbs Live
Verbs only work in these functions:
- fmt.Printf(format string, args …any)
- fmt.Sprintf(…) string → returns formatted string
- fmt.Fprintf(io.Writer, …) → writes to file, buffer, stderr, etc.
Example skeleton:
|
0 1 2 3 4 5 6 |
fmt.Printf("Name: %s, Age: %d, Height: %.1f cm\n", "Webliance", 25, 175.5) |
→ Verbs: %s, %d, %.1f
2. The Four Main Families of Verbs (Official Grouping)
From pkg.go.dev/fmt:
- General — works with almost anything
- Boolean
- Integer
- Floating-point & complex
- String & byte slice
- Pointer
- Special (type, Go-syntax, etc.)
3. Most Useful Verbs — With Examples (Start Here!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package main import "fmt" func main() { name := "Webliance" age := 25 height := 175.5 isPro := true score := 89.75 emoji := '😄' // rune // General / default fmt.Printf("Default: %v\n", name) // Webliance fmt.Printf("Default struct-like: %+v\n", struct{ X int }{42}) // {X:42} fmt.Printf("Go-syntax: %#v\n", height) // 175.5 (shows exact literal) // String & quoted fmt.Printf("String: %s\n", name) // Webliance fmt.Printf("Quoted: %q\n", "Hyderabad!") // "Hyderabad!" // Integer fmt.Printf("Decimal: %d\n", age) // 25 fmt.Printf("Binary: %b\n", 255) // 11111111 fmt.Printf("Hex lower: %x\n", 255) // ff fmt.Printf("Hex upper: %X\n", 255) // FF // Float fmt.Printf("Float default: %f\n", score) // 89.750000 fmt.Printf("1 decimal: %.1f\n", score) // 89.8 fmt.Printf("Scientific: %e\n", score) // 8.975000e+01 // Boolean fmt.Printf("Bool: %t\n", isPro) // true // Type fmt.Printf("Type: %T\n", emoji) // int32 (rune is alias) // Pointer p := &age fmt.Printf("Pointer: %p\n", p) // 0xc0000140a8 (hex address) // Literal % fmt.Printf("Discount: 20%%\n") // Discount: 20% } |
4. Big Cheat-Sheet Table (Most Common Verbs 2026)
| Category | Verb | Description / Output Example | Typical Use Case | Flags/Modifiers Examples |
|---|---|---|---|---|
| General | %v | Default format (smart for most types) | Quick debug, any value | %+v (structs show field names) |
| General | %#v | Go-syntax representation (like literal code) | Debugging structs/slices/maps | — |
| General | %T | Print the type | What type is this? | — |
| General | %% | Literal percent sign (no arg consumed) | Printing % in text | — |
| Boolean | %t | true or false | bool values | — |
| Integer | %d | Decimal (base 10) | Normal integers | %+d (show + sign for positive) |
| Integer | %b | Binary (base 2) | Bit patterns | — |
| Integer | %o | Octal (base 8) | Permissions, old-school | — |
| Integer | %x / %X | Hex (lower/upper) | Colors, hashes, bytes | %#x (add 0x prefix) |
| Integer | %c | Unicode character from code point | rune → char | — |
| Integer | %q | Single-quoted Go literal (for rune) | Safe char printing | — |
| Float | %f | Decimal floating point (default 6 decimals) | Normal floats | %.2f (2 decimals) |
| Float | %e / %E | Scientific notation (lower/upper) | Very large/small numbers | %.2e |
| Float | %g / %G | %e for large exponents, %f otherwise (smart) | General float printing | — |
| String / []byte | %s | Plain string | Text | — |
| String / []byte | %q | Double-quoted Go string literal | Safe string output (escapes) | — |
| String / []byte | %x / %X | Hex dump of bytes (lower/upper) | Binary data, crypto | % x (space separated) |
| Pointer | %p | Hex pointer address (0x…) | Debugging pointers | — |
5. Flags & Width / Precision Modifiers (Make It Pretty!)
Before the verb letter, you can add:
- Width: number → minimum characters (pad with spaces)
- Precision: .number → decimals for float, max width for string
- Alignment: – → left-align
- Sign: + → always show +/-
- Zero pad: 0 → pad with zeros instead of spaces
- Space: → space before positive numbers
Examples:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
fmt.Printf("|%10s|\n", "short") // | short| (right align) fmt.Printf("|%-10s|\n", "short") // |short | (left align) fmt.Printf("|%010d|\n", 42) // |0000000042| (zero pad) fmt.Printf("%+d\n", 42) // +42 fmt.Printf("%.2f\n", 3.14159) // 3.14 fmt.Printf("%10.2f\n", 3.14159) // 3.14 (width 10, right align) fmt.Printf("%-10.2f|\n", 3.14159) // 3.14 | |
6. Your Quick Practice Exercise
Try to produce this output exactly (copy-paste and fix):
|
0 1 2 3 4 5 6 7 8 |
User: Webliance Age: 25 Score: 89.75 Active: true Pointer to age: 0xc000... (your address will differ) Binary 255: 11111111 Hex: ff Quoted "Hyderabad": "Hyderabad" |
Use one Printf line for the first row (use width & precision).
Any verb confusing?
- Want examples with structs, slices, maps, errors?
- How verbs behave with nil, interfaces, custom String() methods?
- Or next: input with Scan, Scanf?
Keep experimenting — formatting verbs are one of the things that make Go debugging & CLI output feel so satisfying once you get comfortable! 💻🇮🇳 You’re doing fantastic — let’s keep going! 🚀
