I am working with JetBrains GoLand and wondering if it's possible to somehow disable the auto removal of unused imports. I have searched the JetBrains forums earlier and there is no such information specifically for Goland.
-
weird "feature" ... also it does the same job at "Actions on Save", therefore you might want to disable "Optimize imports" (&& "Reformat code") there also - rule the things explicitly =) – madzohan Jan 23 '23 at 17:45
-
3The worst feature of goland, i've wasted a lot of time because of it :( – ox160d05d Jan 27 '23 at 08:34
5 Answers
This feature is used so that you do not receive compilation errors for unused imports from Go. You can deactivate the feature via:
Settings (Preferences) > Go > Imports > Optimize imports on the fly
However my recommendation is to leave this as is and instead let the IDE manage the imports for you.
For example, you can start typing template.New
inside the main
function and the IDE will ask which "template" package to import as there are two packages in the standard library "text/template" and "html/template". When only one package is available, that will be imported automatically. When you will remove the last reference to the "template" package, the IDE will remove the import automatically, thus allowing you to run the code without any compilation issues.

- 34,072
- 23
- 111
- 129

- 7,188
- 1
- 35
- 44
-
In newer versions of Goland or Intellij with Go plugin you might find this under "Languages & Frameworks > Go > Imports > Optimize..." – blackgreen Dec 22 '21 at 10:47
-
3The IDE sometimes gets it wrong. For example, if I `import "rsc.io/quote/v4"` and then use `quote.Glass()` in my func, then GoLand deletes the import. But if I carefully not save, then right-click go.mod and do "GoModTidy", then it works fine. In contrast, VSCode even handles the save perfectly and does not delete the import. – xdavidliu Mar 30 '23 at 02:04
-
2I would like to actually get errors instead of some external thing automatically modifying my code. I also like manually formatting the files or places in code that were changed by me. The world is imperfect. I started using GoLand recently in a legacy project and having this option by default just lost us several hours trying to figure it out. – Arsel Muginov Jul 05 '23 at 07:16
Another solution is to name your import as "_". For example: import _ "your/package"
. Doing this will prevent auto removal.

- 1,927
- 19
- 26
-
3This is the only solution that worked for me, even after disabling all the import settings in the options it would still delete the imports as soon as I clicked on the terminal window to type `go get` – sox supports the mods Mar 17 '23 at 16:29
If disabling it doesn't work in File | Settings | Go | Imports | Unchecked Optimized Imports On the fly
You can disable it in the on action Save menu
optimize imports enabled in File | Settings | Tools | Actions on Save | Uncheck Optimized Imports

- 1,317
- 2
- 19
- 31
-
I went to the setting with cmd + comma (on the menu Goland > Settings) – kangkyu Jun 19 '23 at 14:39
The behavior was slightly changed in the 2021.2 version of GoLand and higher (GO-11362).
Previous behavior (2021.1.3 and lower):
Current one (2021.2 and higher):
So, it was slightly updated and there is no reason to disable Optimize imports on the fly, but as dlsniper said you can start typing your code and import packages later. It is a bit convenient way.

- 2,895
- 3
- 17
- 26
-
2so seems like they reverted everything back to `(2021.1.3 and lower)` ... I have `GoLand 2022.2.4` – madzohan Jan 23 '23 at 18:08
-
I like the auto optimization of the imports, but one thing I have noticed, is if I use something that requires an import, and the import gets added just fine, and then later I remove that something and nothing else is using that import, GoLand will grey out the import, but not remove it, and I get compile warnings. Is there a way to have GoLand automatically remove imports no longer in use? – Dan Sherwin Feb 08 '23 at 03:50