Adding multiple returns to existing variables

polaris · · 449 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>When dealing with a single return, it&#39;s easy to do: <code>variable += function()</code>, but it doesn&#39;t seem possible to do something like <code>var1+=, var2+= = multiReturn()</code> without creating temporary variables and then doing the <code>variable += tempVar</code> on its own line.</p> <p>Is there a shorter way to do this?</p> <hr/>**评论:**<br/><br/>rimpy13: <pre><p>Unfortunately, there&#39;s not. Is this something that comes up a lot? One of Go&#39;s design goals was to keep language size down, and this sometimes comes at the cost of an extra line or two of code, especially in less common scenarios like the one you&#39;re describing.</p></pre>code_nomad: <pre><p>Not a lot no, but with how concise go lets you be other places, <code>:=</code> ,<code>defer</code> instead of <code>using()</code>-style blocks, channels instead of mutex nightmare, etc, it feels odd to make throwaway variables.</p></pre>nicerobot: <pre><p>If it was going to be something you needed frequently, one approach I tend to use is simply provide a function that takes the same parameter as the multi-return <a href="https://play.golang.org/p/igDQw-0dmK" rel="nofollow">https://play.golang.org/p/igDQw-0dmK</a></p></pre>

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

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