Go's built-in error is an interface with one method, Error() string. A custom error type is any type of your own that implements that method, usually a struct that carries extra fields such as a code, a field name or a wrapped cause. Use plain errors (errors.New, fmt.Errorf) when callers only need a message, and a custom type when callers must inspect details and make decisions.
Both are the same error interface to the rest of your program, so the choice is about how much structure callers need.
The error type is declared in the language itself:
The simplest way to create one is errors.New, or fmt.Errorf when you need formatting:
Plain errors are easy to write and read. Their weakness is that callers can only print them or compare strings, and matching on message text is fragile.
A sentinel error is a package-level value that callers compare against. Use it when the condition has no extra data.
The %w verb wraps the original error. errors.Is walks the chain of wrapped errors, so the check still works after you add context. Do not compare with == once an error might be wrapped.
A custom type lets you attach structured data. Implement Error() on a struct, and add Unwrap() if it wraps another error.
Return a pointer type with pointer receivers, and keep the function's return type as the error interface. One common trap: never return a typed nil pointer (such as var e *ValidationError that is nil) as an error, because the interface value will not be nil and err != nil becomes true.
errors.As finds the first error in the chain that matches a target type and copies it into your variable, so you can read its fields.
errors.AsType[T] was added in recent Go releases as a generic version of errors.As. If your toolchain predates it, use the form above, which works in every version since Go 1.13.
For several failures at once, errors.Join (Go 1.20 and later) combines errors, and fmt.Errorf accepts multiple %w verbs. errors.Is and errors.As search all of them.
| Need | Use |
|---|---|
| A message for logs or users | errors.New or fmt.Errorf |
| Callers branch on a known condition | Sentinel error plus errors.Is |
| Callers need data (field, code, retry hint) | Custom type plus errors.As |
| Add context while keeping the cause | fmt.Errorf("...: %w", err) |
| Report several failures | errors.Join |
Keep the set of exported error types small. Every exported error type or sentinel becomes part of your package's public API and is hard to change later.
It is a predeclared interface type with a single Error() string method. Any type that has this method satisfies it.
No. Use panic only for programmer errors or unrecoverable states. Expected failures such as bad input or missing records should be returned as errors.
Wrap it with fmt.Errorf("doing X: %w", err). Callers can still use errors.Is and errors.As on the wrapped result.
Slightly, since they allocate a struct, but the difference is rarely meaningful. Clarity for callers matters far more than the allocation.
The built-in error is a one-method interface, and custom error types are simply your own types that implement it. Use plain and sentinel errors for simple cases, custom structs when callers need data, wrap with %w, and inspect with errors.Is and errors.As. Avoid string matching and typed nil returns.