Day 032 — fmt Verbs Deep Dive¶
Month 2 · Week 1 · ⬅ Day 031 · Day 033 ➡ · Journal index
🎯 Learning Objective¶
Master the fmt verbs — the %v/%+v/%#v/%T family, integer/float/string formatting with width/precision/flags — and the Stringer/Formatter hooks plus %w error wrapping.
📚 Topics¶
%v,%+v,%#v,%Tand when each differs- Numeric verbs:
%d %b %o %x %X %c %U, floats%f %e %g - Width, precision, flags (
+,-,0, space,#) fmt.Stringer, argument indexing%[1]d,%w
📖 Reading / Sources¶
-
fmtpackage docs — the verb table - Go blog — The Go fmt package (and the fmt overview)
- The Go Programming Language §7.10 (the
Stringerinterface)
📝 Notes¶
%vis the default format;%+vadds struct field names;%#vprints Go-syntax (re-pasteable) form;%Tprints the dynamic type → [[fmt-v-family]].- Implement
fmt.Stringer(String() string) and%v/%suse it automatically.%#vdeliberately ignoresString()to show the raw value — handy for debugging what something really is. - Integers:
%ddecimal,%bbinary,%ooctal,%x/%Xhex (lower/upper),%cthe rune for a code point,%UtheU+XXXXform.%qquotes strings and runes. - Floats:
%ffixed,%escientific,%gthe compact "use whichever is shorter" form (best default for unknown magnitudes). - Width & precision:
%8.3f= min width 8, 3 decimals. Flags:-left-justify,0zero-pad,+always show sign, space leaves room for a sign,#alternate form (0xprefix, etc.). On%xof a string, the space flag (% x) separates bytes. - Argument indexing
%[1]dreuses the nth argument — great for printing one value in several bases without repeating it. - A verb/type mismatch doesn't panic:
fmt.Printf("%d", "hi")prints%!d(string=hi). Spotting%!in output is a fast bug signal → [[fmt-bad-verb]]. PrintvsPrintlnvsPrintf:Printlnadds spaces between operands and a newline;Printadds spaces only between non-string operands;Printfis fully explicit.- The
Sprint*family returns a string; theFprint*family writes to anyio.Writer(sofmt.Fprintln(os.Stderr, …)andfmt.Fprintf(&builder, …)reuse the same engine). fmt.Errorf("...: %w", err)wraps an error soerrors.Is/errors.Ascan unwrap it — the single most importantfmtidiom for error handling → [[error-wrapping]].
💻 Code Examples¶
type point struct{ X, Y int }
func (p point) String() string { return fmt.Sprintf("(%d,%d)", p.X, p.Y) }
p := point{3, 4}
fmt.Printf("%v\n", p) // (3,4) — uses Stringer
fmt.Printf("%#v\n", p) // main.point{X:3, Y:4} — ignores Stringer
fmt.Printf("%T\n", p) // main.point — dynamic type
fmt.Printf("%[1]d in dec, %[1]x in hex\n", 250) // reuse arg 1
Full code:
examples/month-02/fmt-verbs/main.go· Run:go run ./examples/month-02/fmt-verbs
🏋️ Exercises / Practice¶
| Exercise | Status | Link |
|---|---|---|
Inspect format output (%v/%+v/%#v/%T) in the example |
✅ | examples/month-02/fmt-verbs |
🐛 Mistakes Made¶
- Printed a struct with
%vand got{3 4}with no field names — switched to%+vfor debugging. - Used
%don afloat64and saw%!d(float64=3.14); the%!marker told me the verb was wrong.
❓ Open Questions¶
- When is implementing
fmt.Formatter(full control) worth it over a simpleStringer?
🧠 Active Recall (answer without looking)¶
-
Q: A type has a
String()method. Which verb still shows its underlying Go value?
A
`%#v` — it prints the Go-syntax representation and deliberately ignores `Stringer`. (`%v`/`%s` would call `String()`.) -
Q: What does
fmt.Printf("%d", "oops")produce, and why no panic?
A
`%!d(string=oops)`. `fmt` reports verb/argument mismatches inline with a `%!` marker instead of panicking, so formatting bugs are visible but non-fatal.
🪶 Feynman Reflection¶
fmt verbs are a tiny language for describing how a value should look. %v says "just show me"; %+v says "and label the fields"; %#v says "show me the actual Go code"; %T says "what is this thing?". Width/precision/flags are the fine-print knobs, and %w is the special one that threads an error through so it can be unwrapped later.
🕳️ Knowledge Gaps¶
fmt.Formatter/fmt.GoStringercustom verbs; locale-free number formatting.
✅ Summary¶
I can format any value precisely, hook into output with Stringer, read the %! mismatch markers, and wrap errors with %w.
⏭️ Next Steps / Prep for Tomorrow¶
- Day 033:
strings,strconv, andbytes— the text-wrangling workhorses.
| Time spent | Difficulty | Confidence |
|---|---|---|
| 90 min | 🟦⬜⬜⬜⬜ | 🟦🟦🟦🟦⬜ |
Suggested commit: feat(examples): fmt verbs and Stringer deep dive (day 032)