From: Jonathan Amsterdam Date: Sun, 12 Apr 2020 14:00:27 +0000 (-0400) Subject: errors: add example for Is X-Git-Tag: go1.15beta1~572 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=7b5303d08a1b589708d6bad3d7015d9856273aac;p=gostls13.git errors: add example for Is Add ExampleIs to illustrate how errors.Is works. Updates #31716. Updates #38369. Change-Id: I1b9a6667614635aa3a5ed8b2c108d8eb6f35748b Reviewed-on: https://go-review.googlesource.com/c/go/+/228038 Reviewed-by: Damien Neil --- diff --git a/src/errors/wrap_test.go b/src/errors/wrap_test.go index 590c1857e3..4a4a732c9b 100644 --- a/src/errors/wrap_test.go +++ b/src/errors/wrap_test.go @@ -238,6 +238,19 @@ func (errorUncomparable) Is(target error) bool { return ok } +func ExampleIs() { + if _, err := os.Open("non-existing"); err != nil { + if errors.Is(err, os.ErrNotExist) { + fmt.Println("file does not exist") + } else { + fmt.Println(err) + } + } + + // Output: + // file does not exist +} + func ExampleAs() { if _, err := os.Open("non-existing"); err != nil { var pathError *os.PathError