From 703fb665d68ac96ae193891ffae2774c2a3deb4b Mon Sep 17 00:00:00 2001 From: Jean de Klerk Date: Thu, 16 May 2019 18:15:39 -0600 Subject: [PATCH] errors: update As example to include else case The current example illustrates using As when the error is able to be interpreted as an os.PathError, but elides the "else" case. This CL adds the small extra else case to make it clear that it's not safe to assume As will return true. This CL also squash the err instantiation and the err nil check into one line for brevity. Change-Id: I3d3ab483ffb38fb2788d0498b3f03229a87dd7c3 Reviewed-on: https://go-review.googlesource.com/c/go/+/177717 Reviewed-by: Jonathan Amsterdam Reviewed-by: Damien Neil Run-TryBot: Emmanuel Odeke TryBot-Result: Gobot Gobot --- src/errors/example_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/errors/example_test.go b/src/errors/example_test.go index 7724c16cdf..d7dd782bef 100644 --- a/src/errors/example_test.go +++ b/src/errors/example_test.go @@ -36,11 +36,12 @@ func Example() { } func ExampleAs() { - _, err := os.Open("non-existing") - if err != nil { + if _, err := os.Open("non-existing"); err != nil { var pathError *os.PathError if errors.As(err, &pathError) { fmt.Println("Failed at path:", pathError.Path) + } else { + fmt.Println(err) } } -- 2.50.0