Search code examples
gogoland

How to stop GoLand from auto removal of unused imports?


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.

This picture shows the problem.


Solution

  • 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.