100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Go Error Handling Cheat Sheet

Go Error Handling Cheat Sheet

Covers Go's explicit error return values, wrapping errors with %w, errors.Is and errors.As, custom error types, and panic/recover semantics.

2 PagesIntermediateApr 10, 2026

Basic Error Handling

The idiomatic (result, error) return pattern.

go
func divide(a, b float64) (float64, error) {    if b == 0 {        return 0, errors.New("division by zero")   // Create a simple error    }    return a / b, nil}result, err := divide(10, 0)if err != nil {    fmt.Println("Error:", err)    return}fmt.Println("Result:", result)

Wrapping & Unwrapping Errors

Adding context to errors while preserving the original.

go
var ErrNotFound = errors.New("not found")func findUser(id int) error {    if id < 0 {        return fmt.Errorf("findUser: invalid id %d: %w", id, ErrNotFound)  // %w wraps the error    }    return nil}err := findUser(-1)if errors.Is(err, ErrNotFound) {           // Checks the wrapped chain for a specific sentinel error    fmt.Println("user not found")}var myErr *MyErrorif errors.As(err, &myErr) {                // Extracts a specific error type from the chain    fmt.Println(myErr.Code)}

Custom Errors & panic/recover

Defining error types and handling unrecoverable states.

go
type MyError struct {    Code    int    Message string}func (e *MyError) Error() string {          // Satisfies the error interface    return fmt.Sprintf("[%d] %s", e.Code, e.Message)}func mustPositive(n int) {    if n < 0 {        panic("n must be positive")         // Panic for unrecoverable programmer errors    }}func safeCall() {    defer func() {        if r := recover(); r != nil {       // Recover stops a panic from crashing the program            fmt.Println("recovered:", r)        }    }()    mustPositive(-1)}

Idioms

Conventions the Go community relies on.

  • error interface- type error interface { Error() string }; any type with that method is an error
  • Multiple returns- Go functions typically return (result, error); always check err before using the result
  • errors.New / fmt.Errorf- Create simple errors or formatted errors; %w wraps an inner error
  • errors.Is- Tests whether an error, or any error it wraps, matches a target sentinel error
  • errors.As- Finds the first error in the chain matching a target type and assigns it
  • panic/recover- Used for truly exceptional, unrecoverable situations, not routine error handling
  • defer- Schedules a function call to run when the surrounding function returns (used for cleanup/recover)
Pro Tip

Reserve panic for programmer errors and unrecoverable states — for expected failure conditions like bad input, a missing file, or a network error, always return an error value instead.

Was this cheat sheet helpful?

Explore Topics

#GoErrorHandling#GoErrorHandlingCheatSheet#Programming#Intermediate#BasicErrorHandling#WrappingUnwrappingErrors#Custom#Errors#ErrorHandling#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet