<p>Why does Golang allow unused globals in package main?</p>
<p>And why does it allow unused unexported functions?</p>
<p>Or unused fields?</p>
<hr/>**评论:**<br/><br/>kjk: <pre><p>Because it's rather expensive to prove that global variables, functions or struct fields are unused. Making those checks would make compilation slow.</p>
<p>There are checkers for that: <a href="https://github.com/alecthomas/gometalinter">https://github.com/alecthomas/gometalinter</a></p></pre>xiegeo: <pre><p>That is really cool! Is there a way to use gometalinter on changed code only? Or diff the linter's out put on current work space with last commit as a sort of automatic code review?</p></pre>semi-: <pre><p>As far as I know, no. It's something I've been toying with the idea of, because currently gometalinter just produces so much output it's hard for me to tell if I'm improving things or making them worse.</p>
<p>The ideal fix would be some kind of AST-based diffing to only process packages that are syntactically different, but that would also be the hardest way to do it.</p>
<p>The easiest way I can think of would be to just break the output up in to a directory structure based on package names and maybe linter name, then when you're done linting your entire project you could diff your output to the last output and only show that. I'm not sure if I'll end up going through with that or not though, I've spent more time than I'd like just getting gometalinter and gitlab-ci playing nicely together, so I don't want to spend too much more time on it right now.</p></pre>rr1pp3rr: <pre><p>You may be using the method through reflection. You may be creating a global variable to be used by another package.</p>
<p>Local unused variable never really make sense.</p></pre>dominikh: <pre><blockquote>
<p>You may be using the method through reflection</p>
</blockquote>
<p>You cannot use reflection to call unexported package-level functions.</p>
<blockquote>
<p>You may be creating a global variable to be used by another package</p>
</blockquote>
<p>You cannot import package main.</p></pre>titpetric: <pre><p>You can use go build to compile files selectively from the main package namespace. For example, encode.go and decode.go which would be CLI programs, but wouldn't need each-other.</p>
<p>Also, you can have test-specific code in package main which is used by tests only. Obviously you're not compiling tests along with the application. It's also a valid scenario for package-level functions, at least the <code>gvt</code> package manager removes test files from vendor/ when it pulls dependencies.</p>
<p>Personally, it's a bit of a crutch in the sense you can leave dead code around and collect some tech debt. But as I usually package my logic, I'm not very much exposed to this.</p></pre>xiegeo: <pre><p>So you can build the parts first.</p></pre>sh41: <pre><p>I believe there was an elegant answer by Ian Lance Taylor somewhere, but I can't find it now... :(</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传