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:
I want to add new package to my code.
Then I install it with
dep ensure
and refresh my IDE cache.I can comfortably use the package with IDE autocomplete and navigation.
What happens with goimports
and IDE?
- I add new package
- In order to run
dep ensure
I have to save a file 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.
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 sincegoimports
is invoked on save and because the import is not used it will be automatically removed.