Error Handling in Go

From the Go cheat sheet ยท Error Handling ยท verified Jul 2026

Error Handling

Working with errors in Go

go
// Return error
func divide(x, y float64) (float64, error) {
    if y == 0 {
        return 0, errors.New("division by zero")
    }
    return x / y, nil
}

// Check error
result, err := divide(10, 0)
if err != nil {
    log.Fatal(err)
}
โŒ Errors are values, not exceptions
๐Ÿ”„ Always check returned errors
๐Ÿ“ฆ Wrap errors with context using %w
๐ŸŽฏ errors.Is() and errors.As() for error checking

Continue with Go

Save the full cheat sheet or work through every related task.

More Go tasks

Back to the full Go cheat sheet