golang-pitfalls-error-handling
Installation
SKILL.md
Golang Pitfalls: Error Handling
Source material: mistakes #48-54 from 100 Go Mistakes and How to Avoid Them (teivah/100-go-mistakes).
Apply these rules when handling errors in Go.
48. Panicking (#48)
panicstops the normal flow. Use it sparingly, only for unrecoverable conditions:- Signaling a programmer error (e.g.,
sql.Registerwith a nil/already-registered driver). - Failing to create a mandatory dependency.
- Signaling a programmer error (e.g.,
- In almost all other cases, return a proper error as the last return value instead of panicking.
49. Ignoring when to wrap an error (#49)
- Since Go 1.13,
fmt.Errorf("...: %w", err)wraps an error, making the source error available to the caller. - Wrap to add context (use
%w) or to mark an error as a specific type (create a custom error type). - Wrapping creates coupling — callers can unwrap/compare the source error. If that's unwanted, transform the error instead (
fmt.Errorf("...: %v", err)).