Where did x and y come from here?

blov · · 644 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m trying to understand <a href="https://github.com/google/hilbert/blob/master/hilbert.go#L61" rel="nofollow">this</a> bit of code, specifically, here we use variables <code>x</code>, and <code>y</code> but I can&#39;t see anywhere where they&#39;ve been declared, much less initialized. I thought maybe they become initialized as 0 but that doesn&#39;t seem to work in this example <a href="https://play.golang.org/p/RVB1yZeiYEk" rel="nofollow">here</a> thanks!</p> <hr/>**评论:**<br/><br/>DeedleFake: <pre><p>They&#39;re declared in the return values in the function signature. They, like all declared but unassigned variables in Go, start with zero values in them, which is 0 in this case.</p> <p>Edit: To clarify, like arguments, functions can have named return values. It works like this:</p> <pre><code>func Example() (n int) { return n // Returns 0. } </code></pre></pre>joshlemer: <pre><p>Ah, got it thanks!</p></pre>danmickla: <pre><p>Well, no, they&#39;re the function arguments. &#39;t&#39; and &#39;err&#39; are the return values.</p></pre>DeedleFake: <pre><pre><code>func (s *Hilbert) Map(t int) (x, y int, err error) </code></pre> <p>What? No they&#39;re not. <code>t</code> is an argument. <code>x</code>, <code>y</code>, and <code>err</code> are returns.</p></pre>mborawi: <pre><p>Its works fine if you declare the variables to recieve those values <a href="https://play.golang.org/p/timTW21MlK7" rel="nofollow">https://play.golang.org/p/timTW21MlK7</a></p></pre>SeriousM00nlight: <pre><p>X and Y came from mommy and daddy.</p></pre>

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

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