简体   繁体   中英

How to disable removing unused imports?

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?

PS Unsure about tag godeps but DEP is definitly wrong here.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM