1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package errors implements functions to manipulate errors.
7 // The New function creates errors whose only content is a text message.
9 // The Unwrap, Is and As functions work on errors that may wrap other errors.
10 // An error wraps another error if its type has the method
14 // If e.Unwrap() returns a non-nil error w, then we say that e wraps w.
16 // Unwrap unpacks wrapped errors. If its argument's type has an
17 // Unwrap method, it calls the method once. Otherwise, it returns nil.
19 // A simple way to create wrapped errors is to call fmt.Errorf and apply the %w verb
20 // to the error argument:
22 // errors.Unwrap(fmt.Errorf("... %w ...", ..., err, ...))
26 // Is unwraps its first argument sequentially looking for an error that matches the
27 // second. It reports whether it finds a match. It should be used in preference to
28 // simple equality checks:
30 // if errors.Is(err, fs.ErrExist)
34 // if err == fs.ErrExist
36 // because the former will succeed if err wraps fs.ErrExist.
38 // As unwraps its first argument sequentially looking for an error that can be
39 // assigned to its second argument, which must be a pointer. If it succeeds, it
40 // performs the assignment and returns true. Otherwise, it returns false. The form
42 // var perr *fs.PathError
43 // if errors.As(err, &perr) {
44 // fmt.Println(perr.Path)
49 // if perr, ok := err.(*fs.PathError); ok {
50 // fmt.Println(perr.Path)
53 // because the former will succeed if err wraps an *fs.PathError.
56 // New returns an error that formats as the given text.
57 // Each call to New returns a distinct error value even if the text is identical.
58 func New(text
string) error
{
59 return &errorString
{text
}
62 // errorString is a trivial implementation of error.
63 type errorString
struct {
67 func (e
*errorString
) Error() string {