Execute gofmt on file save in IntelliJ
I use IntelliJ with the Golang plugin. Is it possible to configure the IDE to execute gofmt on file save?
Solution 1:
Of course you can.
- install File Watchers plugin
- open "Preferences->Tools->File Watchers" & Add a new watcher
- set
File Type
:Go
,Program
: the abosolute path ofgofmt
,Arguments
:-w $FilePath$
- click
OK
Solution 2:
Gogland EAP 16 deprecated the On Save actions and replaced them with the File Watchers plugin.
If you have previously configured On Save actions, the IDE will prompt you to install the plugin and configure everything automatically. If you want to set it up from scratch yourself, check if you have File Watchers plugin and if not install it.
Enable gofmt:
- Go to
Settings | Tools | File Watchers
. - Click the
+
button and selectgo fmt
. - The default values are good.
- In Advance Options select:
- Auto-save edited files to trigger the watcher
- Trigger the watcher on external changes
- Click ok!
You can do the same for goimports
and gometalinter
.
Solution 3:
If you are using Gogland, you have an option On Save under Preferences