errors.Is checks whether any error in a wrapped chain equals a target value, and errors.As finds the first error in the chain whose type matches a target type and copies it into a variable. Use errors.Is(err, ErrNotFound) to detect a specific sentinel and errors.As(err, &target) to extract a custom error type and read its fields. Both walk the chain created by fmt.Errorf with %w. This guide covers go errors.Is errors.As with practical examples.
Both functions arrived in Go 1.13 and replaced fragile == comparisons and type assertions that break once an error is wrapped.
When you add context with %w, the new error keeps a reference to the original:
errors.Unwrap returns the next error in the chain. You seldom call it yourself, because errors.Is and errors.As repeatedly unwrap for you. Wrapping with %v or %s instead of %w creates a plain string and cuts the chain.
errors.Is(err, target) returns true if err or any error it wraps is equal to target:
fs.ErrNotExist is the same value as os.ErrNotExist. The *fs.PathError returned by os.ReadFile wraps a system error that matches it.
errors.Is compares each error in the chain with ==, so the types must be comparable. Before comparing, it also checks whether the error has a method Is(target error) bool and calls it. That lets a type define its own equivalence:
Now errors.Is(TimeoutError{"read"}, context.DeadlineExceeded) is true.
errors.As(err, &target) looks for an error assignable to the type of target. If found, it sets target to that error and returns true. target must be a non-nil pointer to a type that implements error or to any interface type.
The variable ve has type *ValidationError, matching the type the function returned. If you declared the error type with a value receiver, you would declare var ve ValidationError instead. Passing a wrong target type, such as a non-pointer, makes errors.As panic, and go vet reports it.
errors.As also accepts a pointer to an interface type, which is useful for behaviours:
The net package errors implement Timeout() bool, so this works across many concrete types.
| Question | Function | Target |
|---|---|---|
| Is it this specific error value? | errors.Is | A sentinel such as io.EOF |
| Is it this kind of error, and what are its fields? | errors.As | Pointer to a variable of the error type |
| Do I just need the message? | Neither | Use err.Error() or log it |
Old style code like err == io.EOF or e, ok := err.(*ValidationError) only inspects the outermost error. After wrapping, both give false results, so replace them.
Since Go 1.20, both functions understand errors that wrap several others, through an Unwrap() []error method. errors.Join and fmt.Errorf with more than one %w create such errors. errors.Is and errors.As search the whole tree depth first:
%v instead of %w, which silently breaks the chain.errors.As, which panics.var ve ValidationError when the code returns *ValidationError.==.err.Error() strings, which breaks when wording changes.== compares only the top-level error. errors.Is also checks every error wrapped inside it and honours custom Is methods.
It must write the matched error into your variable, so it takes the address of that variable. The compiler cannot check this, but go vet does.
Yes, provided the library wraps with %w or implements Unwrap. If it formats the cause into a string, the chain is lost and you cannot match the inner error.
You do not have to. errors.Is(nil, target) returns true only when target is also nil, so with a non-nil target the answer is false.
Use errors.Is to ask whether a chain contains a particular value and errors.As to pull out a particular type and read its data. Wrap with %w so the chain survives, pass a pointer to errors.As, and stop using == or type assertions on errors that may have been wrapped.