Namespace: go.std.errors

v1.0

Contents

Summary

Provides a low-level interface to the errors package.

Package errors implements functions to manipulate errors.

The New function creates errors whose only content is a text message.

The Unwrap, Is and As functions work on errors that may wrap other errors.
An error wraps another error if its type has the method

Unwrap() error

If e.Unwrap() returns a non-nil error w, then we say that e wraps w.

Unwrap unpacks wrapped errors. If its argument's type has an
Unwrap method, it calls the method once. Otherwise, it returns nil.

A simple way to create wrapped errors is to call fmt.Errorf and apply the %w verb
to the error argument:

errors.Unwrap(fmt.Errorf("... %w ...", ..., err, ...))

returns err.

Is unwraps its first argument sequentially looking for an error that matches the
second. It reports whether it finds a match. It should be used in preference to
simple equality checks:

if errors.Is(err, os.ErrExist)

is preferable to

if err == os.ErrExist

because the former will succeed if err wraps os.ErrExist.

As unwraps its first argument sequentially looking for an error that can be
assigned to its second argument, which must be a pointer. If it succeeds, it
performs the assignment and returns true. Otherwise, it returns false. The form

var perr *os.PathError
if errors.As(err, &perr) {
fmt.Println(perr.Path)
}

is preferable to

if perr, ok := err.(*os.PathError); ok {
fmt.Println(perr.Path)
}

because the former will succeed if err wraps an *os.PathError.

Index

Legend

Constants

Constants are variables with :const true in their metadata. Joker currently does not recognize them as special; as such, it allows redefining them or their values.

Variables

Functions, Macros, and Special Forms

Types