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