]> Cypherpunks repositories - gostls13.git/commitdiff
errors: update As example to include else case
authorJean de Klerk <deklerk@google.com>
Fri, 17 May 2019 00:15:39 +0000 (18:15 -0600)
committerEmmanuel Odeke <emm.odeke@gmail.com>
Fri, 17 May 2019 16:21:05 +0000 (16:21 +0000)
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 <jba@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/errors/example_test.go

index 7724c16cdfcf4afbb835fa72804e7fe6ae185a92..d7dd782bef3caea691ff7466acacc329f8ef2c10 100644 (file)
@@ -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)
                }
        }