Errors
error is an interface with one method, Error() string. Functions return it as a last result. A nil error means success; a non-nil error means the caller must handle or wrap the failure.
Detailed Explanation
Go does not throw exceptions for ordinary failure. os.Open returns (*File, error). You check err, then use the file. That is the entire error model at the call site: a value you pass, wrap, and compare. Panics exist for bugs, not for "file not found."
The interface is tiny.
type error interface {
Error() string
}
errors.New("empty name") and fmt.Errorf("empty name") both return an error. The string is for humans and logs. It is not for == against another string, and it is not for control flow. If the caller must branch on the kind of failure, use a sentinel (var ErrEmpty = errors.New(...)) with errors.Is, or a custom type with errors.As.
nil is success. if err != nil is the first line after almost every call that can fail. Handle the error, usually by wrapping it and returning, then continue. Do not use the other results when err != nil unless the function documents that they are still valid.
Sentinels vs types. A sentinel is a package-level var ErrNotFound = errors.New("not found"). Callers write errors.Is(err, ErrNotFound). A custom type carries fields. Callers write errors.As. Do not use err == ErrNotFound once wrapping is in play: %w produces a different value that still unwraps to the sentinel.
Wrapping is how context accumulates. return fmt.Errorf("load config: %w", err) keeps the original error in a chain. errors.Is and errors.As walk that chain. %v or %s in Errorf does not; it only copies the string.
Do not panic for expected failure. A missing file, a bad JSON body, a rejected password — those are errors. A panic is for "this program is wrong": an index out of range you did not expect, an invariant you thought was impossible. Libraries that panic on bad input force every caller to recover.
What it costs. Checking err is a compare and a branch. The cost people complain about is vertical space. The benefit is that every failure is visible in the source. Returning error also means the compiler does not force you to handle it — _ = f() compiles. Treat an ignored error as a bug unless you have a comment that says why.
Code Examples
package main
import (
"errors"
"fmt"
"os"
)
func main() {
f, err := os.Open("go.mod")
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
fmt.Println("opened")
}
var ErrEmpty = errors.New("empty")
func name(s string) error {
if s == "" {
return ErrEmpty
}
return nil
}func load(path string) error {
_, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("load %s: %w", path, err)
}
return nil
}func isEmpty(err error) bool {
return errors.Is(err, ErrEmpty)
}Common Gotchas
A typed nil is not a nil `error`
returning a nil `*MyError` makes `err != nil` true. Return a literal `nil`.
`err == ErrSentinel` fails after wrapping
use `errors.Is`.
The `Error()` string is not an API
do not parse it. Add a type or a sentinel if callers must branch.
Ignoring an error compiles
`os.WriteFile(path, b, 0o644)` as a statement discards the error. That is legal and often wrong.