exp: golang.org/x/exp/errors Index | Examples | Files | Directories

package errors

import "golang.org/x/exp/errors"

Package errors implements functions to manipulate errors.

This package implements the Go 2 draft designs for error inspection and printing:


This is an EXPERIMENTAL package, and may change in arbitrary ways without notice.


if err := oops(); err != nil {


1989-03-15 22:30:00 +0000 UTC: the file system has gone away



Package Files

errors.go format.go frame.go wrap.go

func As Uses

func As(err error, target interface{}) bool

As finds the first error in err's chain that matches a type to which target points, and if so, sets the target to its value and reports success. An error matches a type if it is of the same type, or if it has an As method such that As(target) returns true. As will panic if target is nil or not a pointer.

The As method should set the target to its value and report success if err matches the type to which target points and report success.


_, err := os.Open("non-existing")
if err != nil {
    var pathError *os.PathError
    if errors.As(err, &pathError) {
        fmt.Println("Failed at path:", pathError.Path)


Failed at path: non-existing

func Is Uses

func Is(err, target error) bool

Is returns true if any error in err's chain matches target.

An error is considered to match a target if it is equal to that target or if it implements an Is method such that Is(target) returns true.

func New Uses

func New(text string) error

New returns an error that formats as the given text.

The returned error embeds a Frame set to the caller's location and implements Formatter to show this information when printed with details.


err := errors.New("emit macho dwarf: elf header corrupted")
if err != nil {


emit macho dwarf: elf header corrupted

The fmt package's Errorf function lets us use the package's formatting features to create descriptive error messages.


const name, id = "bimmler", 17
err := fmt.Errorf("user %q (id %d) not found", name, id)
if err != nil {


user "bimmler" (id 17) not found

func Opaque Uses

func Opaque(err error) error

Opaque returns an error with the same error formatting as err but that does not match err and cannot be unwrapped.

func Unwrap Uses

func Unwrap(err error) error

Unwrap returns the next error in err's chain. If there is no next error, Unwrap returns nil.

type Formatter Uses

type Formatter interface {

    // FormatError prints the receiver's first error and returns the next error in
    // the error chain, if any.
    FormatError(p Printer) (next error)

A Formatter formats error messages.

type Frame Uses

type Frame struct {
    // contains filtered or unexported fields

A Frame contains part of a call stack.

func Caller Uses

func Caller(skip int) Frame

Caller returns a Frame that describes a frame on the caller's stack. The argument skip is the number of frames to skip over. Caller(0) returns the frame for the caller of Caller.

func (Frame) Format Uses

func (f Frame) Format(p Printer)

Format prints the stack as error detail. It should be called from an error's Format implementation, before printing any other error detail.

type Printer Uses

type Printer interface {
    // Print appends args to the message output.
    Print(args ...interface{})

    // Printf writes a formatted string.
    Printf(format string, args ...interface{})

    // Detail reports whether error detail is requested.
    // After the first call to Detail, all text written to the Printer
    // is formatted as additional detail, or ignored when
    // detail has not been requested.
    // If Detail returns false, the caller can avoid printing the detail at all.
    Detail() bool

A Printer formats error messages.

The most common implementation of Printer is the one provided by package fmt during Printf. Localization packages such as golang.org/x/text/message typically provide their own implementations.

type Wrapper Uses

type Wrapper interface {
    // Unwrap returns the next error in the error chain.
    // If there is no next error, Unwrap returns nil.
    Unwrap() error

An Wrapper provides context around another error.


fmtPackage fmt implements formatted I/O with functions analogous to C's printf and scanf.

Package errors imports 2 packages (graph) and is imported by 9 packages. Updated 2020-10-09. Refresh now. Tools for package owners.