Running 'goimports' on save in GoLand

matiux picture matiux · Aug 9, 2017 · Viewed 12.5k times · Source

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

Answer

TehSphinX picture TehSphinX · Aug 9, 2017

Adjusted due to new version of Goland:

The setting has meanwhile moved to the File Watchers. Here you can add a new file watcher at the bottom and select goimports from the list.

enter image description here

Setting this to goimports will do both:

  • code formatting (gofmt)
  • updating imports