Unused struct field not causing error

polaris · · 378 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>When there&#39;s an unused variable in the code, the compiler will complain. When there&#39;s an unused struct field, the compiler won&#39;t complain. Why not?</p> <p>My only explanation is because it may be just used by e.g. the json marshaller and may not be mentioned in the code.</p> <hr/>**评论:**<br/><br/>nemith: <pre><p>The field could be in use by other packages. These packages could be future and not even in your GOPATH. </p> <p>Even unexported fields are exposed via reflect. (don&#39;t do this).</p></pre>theo_retiker: <pre><p>So my explanation wasn&#39;t so wrong. But I didn&#39;t thought about libraries, which makes totally sense to not remove unused fields. Thank you :)</p></pre>DualRearWheels: <pre><p>Padding?</p></pre>Morgahl: <pre><p>This. There are numerous places in the Go source that make use of struct padding for alignment reasons across 32 and 64 bit architectures. </p></pre>FuckinStopSayingThis: <pre></pre>Jelterminator: <pre><p>Well, they shouldn&#39;t because it&#39;s not in the spec: <a href="https://github.com/golang/go/issues/10014" rel="nofollow">https://github.com/golang/go/issues/10014</a></p></pre>tv64738: <pre><p>Because that&#39;s an situation that&#39;s much harder to detect, and its consequences are not a very common trap that would deserve explicitly protecting against.</p></pre>robe_and_wizard_hat: <pre><p>I think that&#39;s probably a really good theory as to why it doesn&#39;t get an error.</p></pre>allhatenocattle: <pre><p>Even if it isn&#39;t enforced by the compiler for the reasons below, you might still want to run an analysis tool like <a href="https://github.com/3rf/codecoroner" rel="nofollow">codecoroner</a> to find any dangling fields/functions that aren&#39;t used so they can be removed.</p></pre>theo_retiker: <pre><p>That seems to be a very useful tool. Thanks :D</p></pre>

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

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