All posts
Error Handling Patterns We Actually Use
Wrapping with %w, sentinel errors, errors.As for types — and when a panic is genuinely the right call.
Kampala Gophers Team3 min read
if err != nil is not boilerplate; it's where your program decides what failure means.
var ErrInsufficientFunds = errors.New("insufficient funds")
func (w *Wallet) Withdraw(amount int64) error {
if amount > w.balance {
return fmt.Errorf("withdraw %d: %w", amount, ErrInsufficientFunds)
}
w.balance -= amount
return nil
}
if errors.Is(err, ErrInsufficientFunds) {
// show a friendly message, don't page anyone
}// Weekly Intel
Get the next post in your inbox
Event updates and the occasional Go deep-dive. No spam, unsubscribe any time.