Reputation: 513
I'm using GoLand IDE to code in Go. When I save a file, I'd like that the unused imports disappear (like VS code)
I have enable "Optimize imports" in "Code" -> "Show reformat file dialog". But when I save the file, imports it's still present
Upvotes: 28
Views: 27337
Reputation: 7440
Two separate entries were added to the File Watchers that are enabled by default. One for formatting the code on save, one for optimising the imports on save:
How the imports are sorted, grouped, etc. can be further defined in the Code Style settings for Go:
There is another small option to run gofmt
on reformat in the Other
tab of Go's Code Style settings:
The setting to set up gofmt
or goimports
on save is in the File Watchers. Here you can add a new file watcher at the bottom and select gofmt
or goimports
from the list.
Setting this to goimports
will do both:
gofmt
)Upvotes: 48
Reputation: 71
For those fighting with gofmt on Goland autosave (mat007 comment)
You can double click the item in file watchers to show an edit watcher menu.
Uncheck Auto-save edited files to trigger watcher and it should now only trigger on manual save:
Upvotes: 7
Reputation: 9765
"On Save" tab was removed in GogLand EAP 16.
In newer versions you have to go to Settings
-> Tools
-> File Watchers
and add goimports
there.
Upvotes: 10