Go errors

An error is a value implementing the Error() string method, returned by a function to report that an operation failed. Go code commonly returns an error as the final result and checks it immediately before using the other returned values.

Minimal example

go
func findLimit(key string) (int, error) {
    if key == "" {
        return 0, errors.New("key is required")
    }
    return 10, nil
}

Learn this concept

Lesson 1.7 · Interfaces and Go errors

Used in exercises

Exercise 1.8 · Practice: write your first Go program Exercise 1.11 · Token bucket limiter with lazy refill Exercise 1.14 · Limiter interface and a FixedWindow implementation Exercise 1.18 · RateLimit HTTP middleware that returns 429 Exercise 2.22 · GET /v1/restaurants: signed cursors
Prerequisite conceptsGo functionsGo interfaces