<p>for t, err = p.token(); err == nil; t, err = p. token() {
...
} </p>
<p>So while t is true and error is nil - perform some stuff then call p.token() again. Correct? Or does it matter if t is true as long as err ==nil </p>
<hr/>**评论:**<br/><br/>nhooyr: <pre><p>As long as err == nil. If err is nil, t is assumed to be ok.</p></pre>ops_man: <pre><p>Thanks for the quick response. </p></pre>RalphCorderoy: <pre><p>You could temporarily break it down into a "while" for understanding.</p>
<pre><code>t, err = p.token()
for err == nil {
...
t, err = p. token()
}
</code></pre>
<p>But personally I dislike the repetition in both, with the risk that they'll diverge, and would use</p>
<pre><code>for {
t, err = p.token()
if err != nil {
break
}
...
}
</code></pre></pre>ops_man: <pre><p>Agreed I like your second example. It's legible and doesn't hurt my eyes to look upon. </p></pre>ops_man: <pre><p>Which is faster? Or is their a difference. Which is to say is it faster to check for nil versus non nil value. </p></pre>RalphCorderoy: <pre><p>Compare the assembly output of the compiler for your target. I'd expect there to be no difference.</p></pre>epiris: <pre><p>Hi, this pattern is represented cleanly in text.Scanner if you are the one designing the API. It places the err check outside the loop. If you are consuming another API it doesn't matter much Ralph has it covered in his post.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传