diff options
| author | Marcel van Lohuizen <mpvl@golang.org> | 2019-02-23 00:09:40 +0100 |
|---|---|---|
| committer | Marcel van Lohuizen <mpvl@golang.org> | 2019-02-27 19:09:40 +0000 |
| commit | 62f5e8156ef56fa61e6af56f4ccc633bde1a9120 (patch) | |
| tree | ebec87666eca8e47194fdc2a7431c5a13f4ac3cd /src/errors/example_test.go | |
| parent | 37f84817247d3b8e687a701ccb0d6bc7ffe3cb78 (diff) | |
| download | go-62f5e8156ef56fa61e6af56f4ccc633bde1a9120.tar.xz | |
errors: add Unwrap, Is, and As
Unwrap, Is and As are as defined in proposal
Issue #29934.
Also add Opaque for enforcing an error cannot
be unwrapped.
Change-Id: I4f3feaa42e3ee7477b588164ac622ba4d5e77cad
Reviewed-on: https://go-review.googlesource.com/c/163558
Run-TryBot: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
Diffstat (limited to 'src/errors/example_test.go')
| -rw-r--r-- | src/errors/example_test.go | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/errors/example_test.go b/src/errors/example_test.go index 5dc8841237..7724c16cdf 100644 --- a/src/errors/example_test.go +++ b/src/errors/example_test.go @@ -5,7 +5,9 @@ package errors_test import ( + "errors" "fmt" + "os" "time" ) @@ -32,3 +34,16 @@ func Example() { } // Output: 1989-03-15 22:30:00 +0000 UTC: the file system has gone away } + +func ExampleAs() { + _, err := os.Open("non-existing") + if err != nil { + var pathError *os.PathError + if errors.As(err, &pathError) { + fmt.Println("Failed at path:", pathError.Path) + } + } + + // Output: + // Failed at path: non-existing +} |
