๐ปGo Error Handling Idioms
Stop writing `if err != nil` on autopilot. Learn why Go treats errors as values, then wrap, sentinel, and type them until you can design a clean error story for a small CLI.
Phase 1Errors as Values
See errors as values, not exceptions.
In Go, an error is a value โ nothing more, nothing less
6 minIn Go, an error is a value โ nothing more, nothing less
Any type with one method is an error
6 minAny type with one method is an error
The `if err != nil` ritual is the design, not a flaw
5 minThe `if err != nil` ritual is the design, not a flaw
Panic is for bugs, not for normal failure
6 minPanic is for bugs, not for normal failure
Phase 2Wrapping, Unwrapping, and Comparing
Wrap, unwrap, and compare errors in small programs.
`%w` adds context without hiding the original
6 min`%w` adds context without hiding the original
`errors.Unwrap` peels one layer at a time
5 min`errors.Unwrap` peels one layer at a time
Sentinels are singleton errors you compare against
6 minSentinels are singleton errors you compare against
`errors.Is` walks the chain; `==` does not
5 min`errors.Is` walks the chain; `==` does not
`errors.As` extracts a typed error out of any layer
6 min`errors.As` extracts a typed error out of any layer
Phase 3Choosing the Right Idiom
Choose sentinels, typed errors, errors.Is and errors.As.
Your reader is opening a file that doesn't exist
7 minYour reader is opening a file that doesn't exist
A user reports logs showing "EOF" with no stack of context
7 minA user reports logs showing "EOF" with no stack of context
Users ask: "which file couldn't you parse?"
7 minUsers ask: "which file couldn't you parse?"
Your CLI must exit non-zero on every real failure
7 minYour CLI must exit non-zero on every real failure
Phase 4Designing the CLI's Error Story
Design the error story for a small Go CLI.
Design the error handling for a small file-processing CLI
15 minDesign the error handling for a small file-processing CLI
Frequently asked questions
- Why doesn't Go use try/catch like other languages?
- This is covered in the โGo Error Handling Idiomsโ learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
- What is the difference between errors.Is and errors.As?
- This is covered in the โGo Error Handling Idiomsโ learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
- When should I use a sentinel error versus a typed error?
- This is covered in the โGo Error Handling Idiomsโ learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
- What does %w do in fmt.Errorf?
- This is covered in the โGo Error Handling Idiomsโ learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
- Is panic the Go version of throw?
- This is covered in the โGo Error Handling Idiomsโ learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
Related paths
๐ณDocker Containers Basics
Build the mental model first, then the commands โ from containers vs VMs through images, layers, volumes, and networking to composing a multi-service app.
๐งชProperty-Based Testing
Go beyond example-based tests โ learn to express what your code should always do, then let a framework find the inputs that break it.
๐ฆRust's Ownership Model
Build a working mental model of Rust's ownership system โ from stack vs heap intuition through borrow checker mastery โ so you can read and write Rust without fighting the compiler.
๐ปElixir Pattern Matching
Stop reading `=` as assignment and start using it as Elixir's core flow-control tool โ through function heads, guards, and `with` โ until you can rewrite a tiny command parser without a single `if`.