error
In Go, an ordinary value of the built-in error type that a function returns to report that something went wrong.
In more detail
A function that can fail returns an error as its last result; nil means success. The caller checks it with if err != nil before using any other result, and decides what the failure means. Anything with an Error() string method counts as an error.
Example
if err != nil {
return fmt.Errorf("read config: %w", err)
}Related terms
- nil — The Go value meaning nothing or not set, which is the zero value for pointers, slices, maps, channels, functions, interfaces and errors.
- multiple return values — A Go function returning more than one result at once, most often a useful value together with an error.
- errors.New — The function in Go's errors package that creates a simple error value with a fixed message.
- fmt.Errorf — The fmt function that creates an error with a formatted message, and wraps another error inside it when you use the %w verb.
- error wrapping — Adding context to an error while keeping the original error inside it, so the message explains what failed and code can still test the cause.
- panic — A Go run-time failure that stops normal execution, runs deferred functions and, unless recovered, crashes the program with a stack trace.
Taught in
- What Go Is and Why It Matters — Concurrent Systems Programming with Go
- Go Workspaces and Modules — Concurrent Systems Programming with Go
- Your First Program and go run — Concurrent Systems Programming with Go
- Variables, Typing, and Zero Values — Concurrent Systems Programming with Go
- Errors as Values — Concurrent Systems Programming with Go
- Wrapping Errors and errors.Is — Concurrent Systems Programming with Go
- Panics and Recover — Concurrent Systems Programming with Go
- The Standard testing Package — Concurrent Systems Programming with Go