Error Handling in Go: Chapter Overview

Error Handling is where your programs learn to deal with things going wrong.

In this chapter, you will:

  • create, return and check errors
  • make your own error types
  • wrap errors with context, and look inside with errors.Is and errors.As
  • know when panic and recover are right

What each lesson covers:

  • Error Handling: Errors as values in Go: the error interface, creating, returning and checking errors, custom error types, wrapping with %w, errors.Is and errors.As, and panic and recover.

This chapter has 1 lesson. Plan on about 25 minutes, with 4 exercises to check what you learned. Every example is real Go code: change it, run it again and compare the output, which is the fastest way to learn how each feature behaves.

Simple lesson: finish this chapter and your programs fail clearly instead of silently.

Lessons in this section

  1. Error Handling

    Errors as values in Go: the error interface, creating, returning and checking errors, custom error types, wrapping with %w, errors.Is and errors.As, and panic and recover.

    • Explain the error interface and create errors
    • Return and check errors the Go way
    • Write custom error types and sentinel errors
    • Wrap errors and inspect them with errors.Is and errors.As