From 7b5303d08a1b589708d6bad3d7015d9856273aac Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Sun, 12 Apr 2020 10:00:27 -0400 Subject: [PATCH] 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 --- src/errors/wrap_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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 -- 2.48.1