I use IntelliJ with the Golang plugin. Is it possible to configure the IDE to execute gofmt on file save?
Asked
Active
Viewed 4.7k times
99
-
3I'd recommend using https://godoc.org/golang.org/x/tools/cmd/goimports instead of the standard gofmt. Goimports not only runs gofmt, but it also fixes your imports. – David Brophy Nov 21 '15 at 14:03
5 Answers
149
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

yee
- 1,975
- 1
- 15
- 14
-
6This always reformats every file in my repo, even Godeps/*. So I hardcoded my path instead. Any way to use $FilePath$ without recursively formatting every file in the repo every time? Seems like FilePath is having a domino effect for me . – jayunit100 Nov 28 '15 at 15:10
-
1@jayunit100 You can configure the *Scope* param 1. add a **local scope** 2. name it & set **Pattern** to: file[$proj]:*/&&!file[$proj]:Godeps//*  – yee Nov 30 '15 at 04:04
-
1This doesnt allow me to edit my files. When i try to add a new line to code some more the program brings me back to the previous line and deletes my new line. how do i stop that? note: using phpstorm – bia.migueis Dec 08 '15 at 23:09
-
6never mind, i figured it out. "immediate file sync" will make it so fmt runs as soon as you make any changes. just unselect it and it will only run on file save (or window unfocus) – bia.migueis Dec 08 '15 at 23:30
-
@bia.migueis Yeah, you're right. Immediate file sync must be omitted to fmt only on save. – Zippo Jan 19 '16 at 14:45
-
3Heads up, this answer is somewhat out of date now: the File Watchers plugin now has a pre-built "go fmt" watcher you can add without needing to do much work at all. – Aaron Nov 30 '18 at 18:05
62
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
.

David Miguel
- 12,154
- 3
- 66
- 68
-
9Actually its better to setup only goimports instead of go fmt because it also performs fmt, as specified in the goimports docs: "in addition to fixing imports, goimports also formats your code in the same style as gofmt so it can be used as a replacement for your editor's gofmt-on-save hook" – dux2 May 23 '18 at 09:27
-
2
54

Arun Gopalpuri
- 2,340
- 26
- 27
-
1
-
It's almost the same just like python plugin vs pycharm. The only possible difference is lag on updates. Other then this - it provides you with exactly the same features set. – Alex Pavlenko Apr 28 '17 at 05:37
-
27On Save mechanism is deprecated and will be removed in future. Use File Watchers plugin instead – Zangetsu Nov 06 '17 at 18:41
-
3But even in latest GoLand, I do not see this option. I though when right click on filename, under "Go", I can fmt file. but I do not see the option to enable it during autosave. – curiousengineer Aug 02 '18 at 15:38
3
September 2021 Update.
If you are using GoLand with the 2021.2 version and higher (July 2021), there are a few options to enable gofmt on save besides File Watchers.

s0xzwasd
- 2,895
- 3
- 17
- 26
-
1The "Action on Save" menu item is part of "File Watchers" plugin (https://plugins.jetbrains.com/plugin/7177-file-watchers). Installing the plugin into IDEA Ultimate helped a lot with running go-fmt / go-imports automatically. – ya_pulser Oct 26 '21 at 16:27
2
Updated:
For new versions of the Goland:
- open
File->Settings->Tools->File Watchers
- click on
+
button and choosego fmt
- press
Ok
Done :)

Amin Shojaei
- 5,451
- 2
- 38
- 46