Multiple returns as arguments to a function

xuanbao · · 336 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>This worked the way I had an intuition it would;</p> <pre><code>package main import ( &#34;fmt&#34; &#34;os&#34; &#34;strconv&#34; ) func main() { a, _ := strconv.Atoi(os.Args[1]) b, _ := strconv.Atoi(os.Args[2]) x(y(a, b)) } func y(a, b int) (int, int) { return a+b, a*b } func x(i, j int) { fmt.Printf(&#34;Got %d, %d arguments\n&#34;, i, j) } </code></pre> <p>Are the internals of returning multiple values from a function related to the internals of how multiple arguments get passed to a function?</p> <hr/>**评论:**<br/><br/>anossov: <pre><p><a href="https://golang.org/ref/spec#Calls">https://golang.org/ref/spec#Calls</a></p> <blockquote> <p>As a special case, if the return values of a function or method g are equal in number and individually assignable to the parameters of another function or method f, then the call <code>f(g(parameters_of_g))</code> will invoke f after binding the return values of g to the parameters of f in order. The call of f must contain no parameters other than the call of g, and g must have at least one return value.</p> </blockquote></pre>peterbourgon: <pre><p><del>A function that returns multiple values needs to have those values assigned to variables (or thrown away with underscore). You can&#39;t use a function call that returns 2 arguments as a parameter to a function that takes 2 arguments; you need to use intermediary variables.</del></p> <p>This specific thing is special-cased, as is pointed out above. Mea culpa.</p></pre>ChristophBerger: <pre><p>OP&#39;s code <a href="https://play.golang.org/p/fgtGvCQiFaH" rel="nofollow">does work</a>...</p></pre>peterbourgon: <pre><p>Whoops! You&#39;re right, it&#39;s special cased, as <a href="/u/anossov" rel="nofollow">/u/anossov</a> points out above. Mea culpa.</p></pre>

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

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