Is there a way to extract a single value from a multi-value function return without assignment?

agolangf · · 494 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Say I have:</p> <pre><code>func foo(i int) { //do stuff } </code></pre> <p>and</p> <pre><code>func bar() (int, string) { //do stuff return i, s } </code></pre> <p>How can I do foo(bar()) without having to first a, _ := bar() ?</p> <hr/>**评论:**<br/><br/>peterbourgon: <pre><p>You cannot.</p></pre>aboukirev: <pre><p>Change your <code>foo</code> to</p> <pre><code>func foo(i int, s string) { //do stuff } </code></pre></pre>Robotigan: <pre><p>This isn&#39;t a real answer.</p></pre>Flowchartsman: <pre><p>It’s the only answer you’ll get. Go cannot do what you ask without matching the function signatures. </p></pre>Robotigan: <pre><p>The best answer I got was &#34;no it can&#39;t&#34; and then someone explained why. Changing my question doesn&#39;t answer my question.</p></pre>Flowchartsman: <pre><p>I provide what I hope is a better answer below. </p></pre>: <pre><p>[deleted]</p></pre>exch: <pre><p>Please don&#39;t do this. Trying to be clever just to save yourself one line of code, ends up making for a needlessly complicated and unmaintainable mess. Future you, or whomever has to inevitably read the code later on, will thank you.</p></pre>Robotigan: <pre><p>Wow what a hack. What do you have µ bound to in your editor?</p> <p>Follow up, what&#39;s the reasoning for go not supporting enumerable multi-value returns?</p></pre>nevyn: <pre><p>Same reason <a href="/u/kormoc" rel="nofollow">/u/kormoc</a> got a bunch of downvotes even though he answered the question ... it trades programer read performance for write performance, which Go is against in general.</p> <p>In the above StackOverflow thread the top post talks about a Must() type assertion which is much easier to follow (assuming the value you want to throw away can be thought of in those terms).</p></pre>Flowchartsman: <pre><p>Go strives to avoid that kind of magic as much as possible. For the sake of argument, assume you CAN do this. What are the rules? Does it only work with a unary function with the argument of a single n-coarity function, from which it takes only the first return value? Seems pretty specific to merit a spec change. What happens if the signature of return values changes? Everything breaks, and the reason might not be immediately apparent.</p> <p>Signatures need to match up to avoid weakening the type system more than it already is. This seems verbose, but it’s a good thing. </p></pre>Robotigan: <pre><blockquote> <p>For the sake of argument, assume you CAN do this. What are the rules?</p> </blockquote> <p>I mean, <a href="/u/kormoc" rel="nofollow">/u/kormoc</a>&#39;s hack is pretty unambiguous and that&#39;s basically how it works when returning tuples in python. I get <a href="/u/nevyn" rel="nofollow">/u/nevyn</a>&#39;s answer about readability, and that&#39;s reasonable enough, but it&#39;s not like it&#39;s impossible.</p></pre>Flowchartsman: <pre><p>It <em>is</em>impossible as asked. It cannot be done without runtime type assertions. While it’s unambiguous syntactically, and you can muck about with return values at runtime like the example shows, I’d hardly call it straightforward, and it bypasses any typechecking. You cannot do </p> <p><code>foo(bar())</code></p> <p>Without changing bar’s signature.</p></pre>Robotigan: <pre><p>Okay, I see we have a misunderstanding. I didn&#39;t mean to imply foo(bar()) was my expected sytnax. I was thinking of something like python mutli-variable returns where an enumerrable tuple is return so it&#39;d be something like foo(bar()[1]).</p></pre>

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

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