From bb6616454284d21800d32c1ff3840db9194141af Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Wed, 9 Nov 2011 16:14:18 -0800 Subject: [PATCH] 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 --- doc/effective_go.html | 12 ++++++++++++ doc/effective_go.tmpl | 12 ++++++++++++ 2 files changed, 24 insertions(+) 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