[Dumb Question] How does go runtime differentiate between a struct and its field if a struct is just a alias of the field?

agolangf · · 411 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I am looking at this code</p> <pre><code>type StructWrapper struct { myField InnerWrapper } type InnerWrapper struct { myField int } a := InnerWrapper{1} b := StructWrapper{a} </code></pre> <p>It seems that </p> <pre><code>a.myField a b </code></pre> <p>all have the same address.</p> <p>Upon further research it seems that struct is just an alias to the its underlying field, so I am wondering where is this meta data stored to differentiate the underlying field and the struct itself, and how does golang know that the struct satisfies some interface and not the field satisfy the interface?</p> <hr/>**评论:**<br/><br/>elagergren: <pre><p>The compiler checks whether <code>a.myField</code> is a valid access, not the runtime. Now, it&#39;s true the compiler does store the type information <em>somewhere</em> in the binary so <code>reflect</code> can access fields by names. But in your scenario, the compiler is doing the checking.</p> <p>I&#39;m not sure about the rest of your post. You should read up on the rules of accessing fields and now types implement interfaces.</p></pre>faiface: <pre><p>That&#39;s right, a struct in the memory is just it&#39;s field. </p> <p>So where&#39;s the type info? In fact, when not talking about interfaces, there is no need to store any metadata anywhere.</p> <p>All the type info is checked by the compiler - the compiler knows what struct it is (bc/ code) and translates field accesses to correct address acceses.</p> <p>Now, the only place where we need to know the type during runtime is with interfaces. So, when assigning a value to an interface, compiler also stores the type in the interface value. Note, that this type doesn&#39;t have to be present in the runtime prior to storing in the interface. Compiler knows that you are assigning an int to interface{} and thus generates instructions to store the correct type.</p></pre>patrickdlogan: <pre><p>The only definition in the code that uses space is the int. So the address of the inner struct is the address of the int and the address of the outer struct is also the address of the int.</p></pre>dmanog: <pre><p>but what if I define a struct method</p> <pre><code>func (c * InnerWrapper) some_logic() { } </code></pre> <p>how does the compiler know that </p> <pre><code>type InnerWrapper struct { myField int } a := InnerWrapper{1} a.some_logic // is valid a.myField.some_logic // is not valid </code></pre> <p>if they have the same address, the compiler must store the meta information somewhere right?</p></pre>nhooyr: <pre><p>It doesn&#39;t store the information in the binary. It knows which function to call with which address.</p></pre>ratatask: <pre><p>Perhaps you are mixing up compiling and running ? By analyzing the source code, the compiler knows the type of a and a.myField, and it can therefore fail to compile a.myField.some_logic() as that is invalid. Once you have written a correct program, there doesn&#39;t need to be a check for this at runtime.</p></pre>

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

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