Favourite piece of code

xuanbao · · 353 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hey guys, I wanted to order myself custom mug but couldn&#39;t figure what to put on it, so I thought I&#39;d put some go code on it but don&#39;t know what would be a good snippet that would look interesting but still simple &amp; readable.</p> <p>I ask of you <a href="/r/golang" rel="nofollow">/r/golang</a> users to contribute your favourite pieces of code. It can be anything from interesting designs to small short functions.</p> <hr/>**评论:**<br/><br/>SeerUD: <pre><p>How about the standard:</p> <pre><code>err := drinkCoffee(me) if err != nil { return fmt.Errorf(&#34;failed to drink coffee: %v&#34;, err) } </code></pre></pre>arp242: <pre><p>I think <code>drinkCoffee()</code> should be exported, as drinking coffee outside of the house is a common use case.</p></pre>shovelpost: <pre><p>Realistic example of <a href="https://youtu.be/PAAkCSZUG1c?t=13m41s" rel="nofollow">drinking returning err</a>.</p></pre>nagai: <pre><p>I&#39;d go with a <code>panic</code></p></pre>ESBDB: <pre><p><code> for _, sip := range mugOfCoffee { drink(sip) } panic(&#34;NO MORE COFFEE!!!!!!!!!!!!!!!!&#34;) </code></p></pre>daveddev: <pre><p><a href="https://play.golang.org/p/hQ0-bjp-oW" rel="nofollow">https://play.golang.org/p/hQ0-bjp-oW</a></p> <pre><code>pot := make([][8]coffee, 12) for _, cup := range pot { me.Drink(cup) } </code></pre> <p>This could be added on each iteration:</p> <pre><code> if err := me.Urinate(); err != nil { panic(me.Defecate(err)) } </code></pre></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

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