Go custom errors: wrap with %w, check with Is and As
11 steps · about 3 h 15 min · Intermediate
- Go by Example: Errors (tutorial, Go by Example, 10 min)
Start with the basics of returning and checking errors and a first look at sentinel errors.
- Error handling and Go (article, The Go Blog, 15 min)
It explains the error interface and why errors are plain values, which custom types build on.
- Errors are values (article, The Go Blog, 10 min)
Rob Pike's post gives the mindset behind designing your own error types.
- Working with Errors in Go 1.13 (article, The Go Blog, 25 min)
This is the authoritative explanation of %w, Unwrap, errors.Is and errors.As from the Go team.
- Runnable Go Docs: Custom Errors (doc, Coddy, 20 min)
It has runnable examples of sentinel errors, error types, wrapping with %w, and Unwrap and Is methods.
- Module 8: Error Handling: Idiomatic Patterns (tutorial, Tobias Weiss, 25 min)
It covers the common mistake of leaving out Unwrap, with check questions to test yourself.
- Errors in Go: Wrapping, errors.Is, errors.As, panic and recover (article, Teach YourSelf Coding, 20 min)
It gives a decision table for when to use plain, typed or joined errors, and introduces Go 1.26's errors.AsType.
- Errors in Go: errors.Is, errors.As, Wrapping, and Sentinel Errors (article, HunCoding, 20 min)
It shows the full pattern in a repository and service layer, mapping errors to HTTP status codes.
- Custom Errors and Error Wrapping in Go (article, Calmops, 20 min)
It covers advanced designs, such as a custom type that also matches a sentinel through an Is method.
- Fix: Go Error Handling Not Working — errors.Is, errors.As, and Wrapping (article, FixDevs, 15 min)
It covers the usual bugs: using %v instead of %w, pointer-receiver mismatches and a missing Unwrap.
- errors package - errors - Go Packages (reference, Go standard library, 15 min)
Finish with the official reference for Is, As, AsType, Join and the Unwrap rules.