By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Every error in Go is a value implementing Error() string. That flexibility lets you encode what happened and why in a type, then recover it later without parsing strings. The two patterns are sentinel errors (var ErrInvalidEvent = errors.New(...)) for conditions callers compare against, and typed errors (a struct with fields) when callers need details such as which field failed.
Wrapping is how context travels without destroying the original. fmt.Errorf("parse status: %w", err) builds a new error that still contains the old one; errors.Is walks that chain by identity and errors.As walks it by type. Rule of thumb: %w at the point where you add context, %v when you deliberately want to flatten. Never compare error strings — they are for humans, not control flow.
package main
import (
"errors"
"fmt"
"strconv"
"strings"
)
var ErrInvalidEvent = errors.New("invalid event")
type ValidationError struct {
Field string
Value string
}
func (e *ValidationError) Error() string {
return fmt.Sprintf("invalid %s %q", e.Field, e.Value)
}
func validateStatus(raw string) (int, error) {
status, err := strconv.Atoi(raw)
if err != nil {
return 0, fmt.Errorf("parse status: %w", err)
}
if status < 100 || status > 599 {
return 0, fmt.Errorf("%w: status out of range", ErrInvalidEvent)
}
return status, nil
}
func validateService(name string) error {
if name == "" {
return &ValidationError{Field: "service", Value: name}
}
return nil
}
func main() {
_, err := validateStatus("oops")
fmt.Println("invalid marker:", errors.Is(err, ErrInvalidEvent))
_, err = validateStatus("900")
fmt.Println("invalid marker:", errors.Is(err, ErrInvalidEvent))
err = validateService("")
var validation *ValidationError
found := errors.As(err, &validation)
fmt.Println("typed error:", found, "field:", validation.Field)
_, statusErr := validateStatus("oops")
joined := errors.Join(validateService(""), statusErr)
fmt.Println("joined issues:", len(strings.Split(joined.Error(), "\n")))
}Decide what the caller must do with the failure before choosing the representation. If the caller only needs to know success or failure, return an opaque error. If it needs to branch, expose a sentinel or a predicate. If it needs data, expose a type. On the HTTP boundary you will map these shapes to status codes with a small lookup instead of inspecting strings — that mapping is step 17.
| Tool | Use it for | Example |
|---|---|---|
errors.New | A fixed, comparable condition | ErrInvalidEvent |
fmt.Errorf with %w | Add context while preserving identity | fmt.Errorf("parse status: %w", err) |
fmt.Errorf with %v | Deliberately flatten (log only) | No programmatic matching later |
errors.Is | Check for a sentinel anywhere in the chain | errors.Is(err, ErrInvalidEvent) |
errors.As | Extract a typed error from the chain | errors.As(err, &validation) |
errors.Join | Report independent failures together | errors.Join(errs...) |
Key takeaways
Errors are values; encode what happened in a sentinel or a typed error.
Wrap with %w to add context without losing identity.
Use errors.Is for sentinels and errors.As for typed errors — never match on strings.
errors.Join combines independent failures into one error.
Reserve panic for programmer mistakes, not for input validation.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.