More thoughts on error handling

blov · · 358 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Inspired by this <a href="https://www.reddit.com/r/golang/comments/6a5v1f/error_handling_patterns_in_go/?ref=share&amp;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&#39;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&#39;t like about this is how you have to wrap everything around <code>func() error</code>. I guess you&#39;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&#39;d always get the same result on the playground.</p> <hr/>**评论:**<br/><br/>redditbanditking: <pre><p>Ideally, it&#39;d be nice if we can &#34;watch&#34; 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

358 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传