]> Cypherpunks repositories - gostls13.git/commitdiff
effective_go: a little more about comma ok and type assertion
authorRob Pike <r@golang.org>
Thu, 10 Nov 2011 00:14:18 +0000 (16:14 -0800)
committerRob Pike <r@golang.org>
Thu, 10 Nov 2011 00:14:18 +0000 (16:14 -0800)
Fixes #2416.

R=golang-dev, iant
CC=golang-dev
https://golang.org/cl/5370049

doc/effective_go.html
doc/effective_go.tmpl

index bec95e5fb602895b918f927434adf7e346308bcb..f0d0ffa53bf6de487be4caf8e38074376f31f4cc 100644 (file)
@@ -2719,6 +2719,18 @@ for try := 0; try &lt; 2; try++ {
 }
 </pre>
 
+<p>
+The second <code>if</code> statement here is idiomatic Go.
+The type assertion <code>err.(*os.PathError)</code> is
+checked with the "comma ok" idiom (mentioned <a href="#maps">earlier</a>
+in the context of examining maps).
+If the type assertion fails, <code>ok</code> will be false, and <code>e</code>
+will be <code>nil</code>.
+If it succeeds,  <code>ok</code> will be true, which means the
+error was of type <code>*os.PathError</code>, and then so is <code>e</code>,
+which we can examine for more information about the error.
+</p>
+
 <h3 id="panic">Panic</h3>
 
 <p>
index 69a16239a11f1889d59d1979510663057aebe5a6..b9ba469d4121a214d634b8fd4671ab3b8db50c6c 100644 (file)
@@ -2657,6 +2657,18 @@ for try := 0; try &lt; 2; try++ {
 }
 </pre>
 
+<p>
+The second <code>if</code> statement here is idiomatic Go.
+The type assertion <code>err.(*os.PathError)</code> is
+checked with the "comma ok" idiom (mentioned <a href="#maps">earlier</a>
+in the context of examining maps).
+If the type assertion fails, <code>ok</code> will be false, and <code>e</code>
+will be <code>nil</code>.
+If it succeeds,  <code>ok</code> will be true, which means the
+error was of type <code>*os.PathError</code>, and then so is <code>e</code>,
+which we can examine for more information about the error.
+</p>
+
 <h3 id="panic">Panic</h3>
 
 <p>