From: Rob Pike Date: Thu, 10 Nov 2011 00:14:18 +0000 (-0800) Subject: effective_go: a little more about comma ok and type assertion X-Git-Tag: weekly.2011-11-18~123 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=bb6616454284d21800d32c1ff3840db9194141af;p=gostls13.git effective_go: a little more about comma ok and type assertion Fixes #2416. R=golang-dev, iant CC=golang-dev https://golang.org/cl/5370049 --- diff --git a/doc/effective_go.html b/doc/effective_go.html index bec95e5fb6..f0d0ffa53b 100644 --- a/doc/effective_go.html +++ b/doc/effective_go.html @@ -2719,6 +2719,18 @@ for try := 0; try < 2; try++ { } +

+The second if statement here is idiomatic Go. +The type assertion err.(*os.PathError) is +checked with the "comma ok" idiom (mentioned earlier +in the context of examining maps). +If the type assertion fails, ok will be false, and e +will be nil. +If it succeeds, ok will be true, which means the +error was of type *os.PathError, and then so is e, +which we can examine for more information about the error. +

+

Panic

diff --git a/doc/effective_go.tmpl b/doc/effective_go.tmpl index 69a16239a1..b9ba469d41 100644 --- a/doc/effective_go.tmpl +++ b/doc/effective_go.tmpl @@ -2657,6 +2657,18 @@ for try := 0; try < 2; try++ { } +

+The second if statement here is idiomatic Go. +The type assertion err.(*os.PathError) is +checked with the "comma ok" idiom (mentioned earlier +in the context of examining maps). +If the type assertion fails, ok will be false, and e +will be nil. +If it succeeds, ok will be true, which means the +error was of type *os.PathError, and then so is e, +which we can examine for more information about the error. +

+

Panic