Handle errors idiomatically in Go with wrapping and sentinels
8 steps · about 1 h 55 min · Intermediate
- Start from errors as values (article, The Go Blog, 15 min)
Sets the mindset behind Go's if err != nil style.
- Wrap errors and inspect them (article, The Go Blog, 20 min)
Introduces %w, errors.Is and errors.As.
- Read the errors package reference (article, pkg.go.dev, 15 min)
Gives exact semantics for Is, As, Join and Unwrap.
- See basic errors by example (article, Go by Example, 10 min)
Shows sentinel and custom error code compactly.
- Write a custom error type (article, Go by Example, 10 min)
Adds structured fields to an error.
- Know when panic and recover apply (article, The Go Blog, 20 min)
Clarifies the rare cases where panic is acceptable.
- Recover from a panic by example (article, Go by Example, 10 min)
Shows the rare recover pattern in a few lines.
- Design APIs that cut error boilerplate (article, The Go Blog, 15 min)
Shows patterns that reduce repeated checks.