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:

Go

→ Verbs: %s, %d, %.1f

2. The Four Main Families of Verbs (Official Grouping)

From pkg.go.dev/fmt:

  1. General — works with almost anything
  2. Boolean
  3. Integer
  4. Floating-point & complex
  5. String & byte slice
  6. Pointer
  7. Special (type, Go-syntax, etc.)

3. Most Useful Verbs — With Examples (Start Here!)

Go

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:

Go

6. Your Quick Practice Exercise

Try to produce this output exactly (copy-paste and fix):

text

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! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *