]> Cypherpunks.ru repositories - gostls13.git/commitdiff
errors: add example for Is
authorJonathan Amsterdam <jba@google.com>
Sun, 12 Apr 2020 14:00:27 +0000 (10:00 -0400)
committerJonathan Amsterdam <jba@google.com>
Mon, 13 Apr 2020 20:04:56 +0000 (20:04 +0000)
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 <dneil@google.com>
src/errors/wrap_test.go

index 590c1857e376558af7625eed964aaff4533d340c..4a4a732c9be07436d72f07aa0c3c1917daf544fb 100644 (file)
@@ -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