Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 727
Myles McDonnell Avatar asked Nov 18 '15 08:11

Myles McDonnell


People also ask

How do I run a Goimports file?

goimports If your project does not have goimports, click the go get goimports link in the Goimports file notification window. Otherwise, open the Terminal tool window (View | Tool Windows | Terminal), and type the following command to install goimports: go get golang.org/x/tools/cmd/goimports . Press Enter .

How do I set up Gofmt?

If your version of GoLand 2021.2 or higher, you can enable Run gofmt on code reformat option under Preferences/Settings | Editor | Code Style | Go | Other (it is enabled by default). After that, execute Code | Reformat Code ( Option + Command + L on macOS) and gofmt will be executed after internal GoLand formatter.


1 Answers

Of course you can.

  1. install File Watchers plugin
  2. open "Preferences->Tools->File Watchers" & Add a new watcher
  3. set File Type: Go, Program: the abosolute path of gofmt, Arguments: -w $FilePath$
  4. click OK

enter image description here

like image 100
yee Avatar answered Oct 06 '22 00:10

yee