Go handles errors with explicit return values, not exceptions. A function that can fail returns an error as its last result, and the caller checks whether it is nil before using the other results. Errors are ordinary values that you create, wrap, compare and inspect with normal code.
This keeps control flow visible: every place a failure can happen is a line you can read. Since Go 1.13 the standard library also supports wrapping errors with %w and inspecting them with errors.Is and errors.As.
error interface and returning errorsfmt.Errorf and %werrors.Is and errors.Aserror interface and returning errorserror is a built-in interface with a single method:
By convention, a function returns (result, error), and the result is only meaningful when the error is nil.
The caller checks err != nil immediately after the call. This pattern is the core of idiomatic Go.
fmt.Errorf and %wA bare message like "no such file" is hard to debug several layers up. Use fmt.Errorf with the %w verb to add context while keeping the original error available for inspection.
Use %v instead of %w when you deliberately do not want callers to depend on the underlying error. Go 1.20 and later also lets one fmt.Errorf call wrap several errors with multiple %w verbs, and errors.Join combines a list of errors into one.
errors.Is and errors.AsDo not compare wrapped errors with ==. Use errors.Is to test for a specific sentinel value and errors.As to extract a specific error type anywhere in the chain.
Sentinel errors such as io.EOF or fs.ErrNotExist suit simple, stable conditions. Custom error types suit cases where the caller needs data, such as which field failed.
Handle the failure first and keep the happy path unindented.
In production code, use a client with a timeout or pass a context.Context through http.NewRequestWithContext rather than the default client.
Either handle an error (log it, retry, show it to the user) or return it, not both. Logging and returning produces duplicate log lines at every layer. Wrap with context as it travels up, and log it once at the boundary, for example in the HTTP handler or main.
panic stops normal execution and unwinds the stack, and recover inside a deferred function can stop it. Reserve them for programmer errors and unrecoverable states, such as an impossible internal condition. Libraries should not let panics cross their public API. Expected failures like bad input, missing files or network errors are always returned as error values.
No. Go has no exceptions. Functions return errors and callers check them, while panic and recover exist only for exceptional situations.
if err != nil repeated so often?It makes every failure point explicit and reviewable. You can reduce noise by structuring code into small functions and wrapping errors with context rather than hiding them.
errors.Is versus errors.As?Use errors.Is to match a specific error value, and errors.As to find an error of a given type and read its fields.
pkg/errors package?Not for new code. Standard fmt.Errorf with %w, errors.Is, errors.As and errors.Join cover wrapping and inspection, and pkg/errors is no longer maintained.
Go treats errors as values returned alongside results. Check err right after each call, wrap with fmt.Errorf and %w to add context, and inspect with errors.Is and errors.As. Handle each error once, and keep panic for truly unrecoverable bugs.