Doesn't order of declaration matter?

xuanbao · · 491 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Here&#39;s a relatively smaller code. This is part of parsing a <code>yaml file</code>. This code to define structure to parse a yaml file. It works but I would like to know why!</p> <pre><code>type GLBConfig struct { GLBs []GLBList `json:&#34;glb_list&#34; yaml:&#34;glb_list&#34;` } type GLBList struct { Failover string `json:&#34;failover&#34; yaml:&#34;failover&#34;` GLB string `json:&#34;glb&#34; yaml:&#34;glb&#34;` Pool []struct { Fqdn string `json:&#34;fqdn&#34; yaml:&#34;fqdn&#34;` PercentConsidered int `json:&#34;percent_considered&#34; yaml:&#34;percent_considered&#34;` } `json:&#34;pool&#34; yaml:&#34;pool&#34;` } </code></pre> <p>The confusion is that, I&#39;ve used <code>GLBList</code> in the struct <code>GLBConfig</code> even before defining it? Doesn&#39;t order of defining matter?</p> <p>Thanks</p> <hr/>**评论:**<br/><br/>drvd: <pre><p>No.</p></pre>neonsteven: <pre><p>Well put. Let me try to expand a bit :).</p> <p>The <a href="https://golang.org/ref/spec#Declarations_and_scope">language spec</a> answers the question in a fairly technical way.</p> <blockquote> <p>The scope of an identifier denoting a constant, type, variable, or function (but not method) declared at top level (outside any function) is the package block.</p> </blockquote> <p>In other words, when you say &#34;type foo struct {}&#34; then &#34;foo&#34; is defined everywhere in the package, including earlier in the same file and in other files.</p></pre>shadyabhi: <pre><p>Thanks! That was helpful. Appreciate the spec reference.</p></pre>kpmy: <pre><p>This is kind of automatic &#34;forward declaration&#34; feature.</p></pre>shadyabhi: <pre><p>As per FAQ, golang doesn&#39;t have forward declaration. <a href="https://golang.org/doc/faq" rel="nofollow">https://golang.org/doc/faq</a></p></pre>bmurphy1976: <pre><p>This is why: <a href="https://en.wikipedia.org/wiki/Multi-pass_compiler" rel="nofollow">https://en.wikipedia.org/wiki/Multi-pass_compiler</a></p></pre>natefinch: <pre><p><a href="http://static.guim.co.uk/sys-images/Money/Pix/pictures/2013/11/14/1384434812854/Milla-Jovovich-with-her-M-008.jpg" rel="nofollow">multipass</a></p></pre>

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

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