fmt printing
Use `fmt` deliberately: choose verbs carefully, separate formatting from logging, and prefer simple output APIs when formatting is not the point.
Canonical guidance
- choose the simplest
fmtAPI that matches the job - use verbs intentionally
- implement
String()only when the string form is genuinely useful
Use when
- building human-readable messages
- debugging values locally
- implementing textual representations
Avoid
fmt.Sprintffor trivial concatenation- production logging with scattered
fmt.Printf - guessing verbs until output “looks right”
Preferred pattern
msg := fmt.Sprintf("user %q not found", id)
Anti-pattern
fmt.Printf("%s", someStruct)
Explanation: This is tempting because many values print somehow, but wrong verbs hide bugs and produce misleading output.
Why
- formatting code is small, high-frequency, and easy to get subtly wrong
Related pages
Sources
- fmt package - Go Team
- Effective Go - Go Team