Pragmatic Error Handling in Go
Beyond if err != nil - techniques that work.
Always Wrap
return fmt.Errorf("get order %s: %w", id, err)
Sentinels
var ErrNotFound = errors.New("not found")
HTTP Pattern
type HandlerFunc func(w http.ResponseWriter, r *http.Request) error
errgroup
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error { return fn() })
If this was helpful, you can support my work at ko-fi.com/nopkt
If this article helped you, consider buying me a coffee on Ko-fi! Follow me for more production backend patterns.
Top comments (0)