Stage 3 · Build
Go Fundamentals for SREs
Error Handling Patterns
Sentinel errors, wrapping, fmt.Errorf, and error inspection for production reliability.
Errors Are Values
In Go, errors are values — not exceptions. Every function that can fail returns an error value alongside its result. The caller checks the error explicitly. There is no try-catch, no stack unwinding, no hidden control flow.
Sentinel Errors
Sentinel errors are package-level variables that define specific error conditions. Callers can compare against them using errors.Is to determine the exact failure reason.
The convention is ErrSomething — ErrNotFound, ErrTimeout, ErrPermissionDenied. This makes them instantly recognizable as error values when reading code.
Error Wrapping with %w
Error wrapping adds context to errors as they propagate up the call stack. The %w verb in fmt.Errorf preserves the original error chain while adding a descriptive message.
Error Inspection
errors.Is checks if any error in the chain matches a target. errors.As extracts a specific error type from the chain. These are the tools for handling wrapped errors correctly.
Custom Error Types
When sentinel errors are not enough, implement the error interface on a struct. Custom error types can carry structured data — HTTP status codes, error codes, retry hints — that callers can use programmatically.
Error Handling Patterns
Production error handling follows patterns. Handle errors at the right level, add context at each boundary, and never silently swallow errors.
The _ = err pattern hides bugs. If you genuinely cannot handle an error, log it and explain why. Silent failures are the hardest bugs to diagnose in production.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.