imported and not used #1337

polaris · · 697 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I regularly push code to my repository only to find out I forgot to remove some imports. Any tips on how to avoid this issue? Pre commit hook? On save clean imports? How do you guys handle this? I&#39;m using Gogland-EAP / IntelliJ.</p> <hr/>**评论:**<br/><br/>oylenshpeegul: <pre><p>I configured my editor to run <a href="https://godoc.org/golang.org/x/tools/cmd/goimports" rel="nofollow">goimports</a> on save.</p></pre>funny_falcon: <pre><p>Don&#39;t push without testing.</p></pre>WellAdjustedOutlaw: <pre><p>I wish I could upvote this 100 times.</p></pre>dlsniper: <pre><p>You can use the following steps to avoid this: open Code | Show Reformat File Dialog then check the Optimize imports box in the popup. Now, every time you&#39;ll use the builtin formatter of the IDE it will remove the unused imports.</p> <p>You could also run goimports by using the shortcut assigned to it, see Code | Go Tools | goimports file from time to time, it will save the file then run the goimports tool (the IDE will help you install it if it&#39;s missing from your system).</p> <p>Hope it helps.</p> <p>Edit: there&#39;s also an issue opened on the tracker to make this the default option in the IDE, please vote / star <a href="https://youtrack.jetbrains.com/issue/GO-3147" rel="nofollow">https://youtrack.jetbrains.com/issue/GO-3147</a> to know when this gets solved. Thank you.</p></pre>jns111: <pre><p>I followed your suggestion and found out Goglang offers Reformat Imports as an option in the push dialog too. Problem solved. Thanks!</p></pre>dlsniper: <pre><p>Also please know that you should run gofmt on the commit step if you want to ensure that your files are formatted accordingly as currently the internal formatter may have a few differences compared to it.</p> <p>Have fun using the IDE.</p></pre>qftvfu: <pre><p>Atom with go-plus will automagically remove unused imports when you save your file.</p></pre>dlsniper: <pre><p>So will Gogland, as described above. Please follow the instructions and the magic will happen. If you have any issues, please let me know. Thanks. </p></pre>chrisfu: <pre><p>...and sometimes automagically removes imports that you need. Had that exact issue last week with os/user. It&#39;s rare but occasionally happens. Solved by aliasing the import.</p> <p>Whilst it&#39;s a great tool to have available, and one that I&#39;d recommend to everyone, it still pays to keep an eye on what it&#39;s doing.</p></pre>gohacker: <pre><p><a href="https://golang.org/misc/git/pre-commit" rel="nofollow">https://golang.org/misc/git/pre-commit</a></p> <p>But with gofmt/goimports on save you might never need that. You can still add it, as a belt and suspenders measure.</p> <p>Also, if you use a CI, you might want to check if the output of <code>gofmt -l .</code> is empty.</p></pre>

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

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