Back to library

๐Ÿ’ป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.

Foundations14 drops~2-week path ยท 5โ€“8 min/daytechnology

Phase 1Errors as Values

See errors as values, not exceptions.

4 drops
  1. In Go, an error is a value โ€” nothing more, nothing less

    6 min

    In Go, an error is a value โ€” nothing more, nothing less

  2. Any type with one method is an error

    6 min

    Any type with one method is an error

  3. The `if err != nil` ritual is the design, not a flaw

    5 min

    The `if err != nil` ritual is the design, not a flaw

  4. Panic is for bugs, not for normal failure

    6 min

    Panic is for bugs, not for normal failure

Phase 2Wrapping, Unwrapping, and Comparing

Wrap, unwrap, and compare errors in small programs.

5 drops
  1. `%w` adds context without hiding the original

    6 min

    `%w` adds context without hiding the original

  2. `errors.Unwrap` peels one layer at a time

    5 min

    `errors.Unwrap` peels one layer at a time

  3. Sentinels are singleton errors you compare against

    6 min

    Sentinels are singleton errors you compare against

  4. `errors.Is` walks the chain; `==` does not

    5 min

    `errors.Is` walks the chain; `==` does not

  5. `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.

4 drops
  1. Your reader is opening a file that doesn't exist

    7 min

    Your reader is opening a file that doesn't exist

  2. A user reports logs showing "EOF" with no stack of context

    7 min

    A user reports logs showing "EOF" with no stack of context

  3. Users ask: "which file couldn't you parse?"

    7 min

    Users ask: "which file couldn't you parse?"

  4. Your CLI must exit non-zero on every real failure

    7 min

    Your 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.

1 drop
  1. Design the error handling for a small file-processing CLI

    15 min

    Design 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.