How to disable removing unused imports?

我是研究僧i 提交于 2019-12-08 02:19:56

问题


I use goimports to automatically reformat my code and make it clean and also keep import in order. But sometimes it works in an incomfortable way together with IDE autosave. Like in this scenario:

  1. I want to add new package to my code.

  2. Then I install it with dep ensure and refresh my IDE cache.

  3. I can comfortably use the package with IDE autocomplete and navigation.

What happens with goimports and IDE?

  1. I add new package
  2. In order to run dep ensure I have to save a file
  3. goimports finds unused package and removes it from imports. Not good.

Workaround:

A. Add new package manually with dep ensure -add %packagename%

B. Disable goimports and move to gofmt - so-so.

C. Do not autoformat code on save - undesirable.

How to setup things more convenient?

P.S. Unsure about tag godeps but DEP is definitly wrong here.


回答1:


The correct solution would be to invoke dep ensure -add package/name yourself and then just start using the package without manually importing it, the IDE will figure out that you want to use that package and will add the import automatically. The current behavior you are experiencing is exactly what's expected from the IDE since goimports is invoked on save and because the import is not used it will be automatically removed.



来源:https://stackoverflow.com/questions/47154405/how-to-disable-removing-unused-imports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!