<p>Any gophers here approve of using something like a 'check' macro that can be used to hide err checking spaghetti code?</p>
<p>Example:</p>
<p>#define check(err, call) { err = call; if (err) return err; }</p>
<p>Usage:</p>
<p>check(err, connect(context));</p>
<hr/>**评论:**<br/><br/>bbrazil: <pre><p>Some libraries have a <a href="http://golang.org/pkg/text/template/#Must" rel="nofollow">Must function</a> or <a href="https://godoc.org/github.com/prometheus/client_golang/prometheus#MustRegister" rel="nofollow">variant</a> that panics if an error is returned.</p></pre>dchapes: <pre><p>Which of course are intended for use almost exclusively in global variable initializations and such. Just as with regular panics being abused where there should instead be error returns or clean exits (e.g. via <code>log.Fatal</code>) I've seen these abused as well.</p></pre>tdewolff: <pre><p>To be honest, I don't have that many error-checking ifs throughout my code. I mean I do check every error, but it doesn't bother much when you properly structure your code. Anyways, most of my error checking statements are unique. Either in the error string or by handling other assets.</p>
<p>However, for something like many sequential writes to an <code>io.Writer</code> I can understand why you would come up with this. But check <a href="https://talks.golang.org/2013/bestpractices.slide#5" rel="nofollow">https://talks.golang.org/2013/bestpractices.slide#5</a> for a solution.</p></pre>GoGoGadgetGopher: <pre><p>you can use context-sensitive code folding in your editor if you really hate looking at the error checking all the time</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传