Running 'goimports' on save in GoLand

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


As of version 2021.3

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:

enter image description here

How the imports are sorted, grouped, etc. can be further defined in the Code Style settings for Go:

enter image description here

There is another small option to run gofmt on reformat in the Other tab of Go's Code Style settings:

enter image description here

Before version 2021.3

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.

enter image description here

Setting this to goimports will do both:

  • code formatting (gofmt)
  • updating, sorting and grouping imports

Tags:

Go

Goland