<p>Inspired by this <a href="https://www.reddit.com/r/golang/comments/6a5v1f/error_handling_patterns_in_go/?ref=share&ref_source=link" rel="nofollow">post</a>, so I tried to come up with my own generic pattern for error handling.</p>
<p>Very loosely inspired by the Javascript <code>then</code> construct, here's what I come up (with examples):
<a href="https://play.golang.org/p/Y3NYjrFJ54" rel="nofollow">https://play.golang.org/p/Y3NYjrFJ54</a></p>
<p>The one thing I don't like about this is how you have to wrap everything around <code>func() error</code>. I guess you'd just have to live with it unless Go can support something like this natively.</p>
<p>Thoughts and opinions?</p>
<p>edit: FYI, go playground random is not seeded, so you'd always get the same result on the playground.</p>
<hr/>**评论:**<br/><br/>redditbanditking: <pre><p>Ideally, it'd be nice if we can "watch" a variable.</p>
<pre><code>var err error
watch err {
a, b, err := foo()
e, err := bar()
f, err := stuff()
} if err != nil {
fmt.Println(err)
}
</code></pre>
<p>Execution inside the watch block stops as soon as err values satisfies one of the if condition.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传