Commit bb661645 authored by Rob Pike's avatar Rob Pike

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
parent e4eacf39
...@@ -2719,6 +2719,18 @@ for try := 0; try < 2; try++ { ...@@ -2719,6 +2719,18 @@ for try := 0; try < 2; try++ {
} }
</pre> </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> <h3 id="panic">Panic</h3>
<p> <p>
......
...@@ -2657,6 +2657,18 @@ for try := 0; try &lt; 2; try++ { ...@@ -2657,6 +2657,18 @@ for try := 0; try &lt; 2; try++ {
} }
</pre> </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> <h3 id="panic">Panic</h3>
<p> <p>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment