goland how to use gofmt?
Asked Answered
I

5

101

I started learning go and installed GoLand from JetBrains. I am reading the book The Go Programming Language, and in the first chapter author strongly recommends using the gofmt tool before each save. How I can follow this recommendation in GoLand?

Impeller answered 10/12, 2017 at 3:49 Comment(1)
Personally, I required the Goland to execute go fmt before committing, it's in the commit setting.Virtuous
B
89

GoLand have commands to go fmt your file or project. Right click on your file and you will find it under "Go tools". You could see its shortcut there.

You can also use "Reformat code" command (bound to Command-Alt-L for me). It's not 100% same as go fmt but very close. Also works for other languages.

If you have an open terminal, you can run go fmt ./... to format all files in a directory (including sub directories). You can put this in a git commit hook to run it every time you commit.

As others mentioned there's a file watcher section under Preferences → Tools → File Watchers, in there you can setup to run go fmt or goimports everytime you save a Go file.

Blane answered 10/12, 2017 at 8:27 Comment(2)
You can also do it every time you save using File Watchers pluginZeppelin
When working with GoLand most of the time I have a terminal window open below my code and just time "go fmt" in there if my code feels cluttered.Tabaret
L
137

You can do that in one of these two ways:

  1. For Goland: preferences ->Tools ->File Watchers and enable go fmt. This way on each saves it will format the file.

  2. once your code is done. just run the command gofmt -s -w . in the terminal in the required directory or else in the needed file. it will format your whole directory/file as per your need.

Loggins answered 10/12, 2017 at 16:49 Comment(1)
docs: -s - try to simplify, -w - do not print reformatted sources to standard output.Semidiurnal
B
89

GoLand have commands to go fmt your file or project. Right click on your file and you will find it under "Go tools". You could see its shortcut there.

You can also use "Reformat code" command (bound to Command-Alt-L for me). It's not 100% same as go fmt but very close. Also works for other languages.

If you have an open terminal, you can run go fmt ./... to format all files in a directory (including sub directories). You can put this in a git commit hook to run it every time you commit.

As others mentioned there's a file watcher section under Preferences → Tools → File Watchers, in there you can setup to run go fmt or goimports everytime you save a Go file.

Blane answered 10/12, 2017 at 8:27 Comment(2)
You can also do it every time you save using File Watchers pluginZeppelin
When working with GoLand most of the time I have a terminal window open below my code and just time "go fmt" in there if my code feels cluttered.Tabaret
E
5

Just want to add 1 more option. You can automatically apply go fmt every time when you are about to commit to git. When you open the commit window, under the Before Commit section, there is a Go fmt checkbox. Just check it. Later when you do another commit, the default value for that checkbox is checked. So, you only need to check it once. Then go fmt will run automatically before you commit.

Electroshock answered 5/4, 2019 at 3:50 Comment(1)
This could also be set in Preference > Version Control > CommitCavender
C
4

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

enter image description here

After that, execute Code | Reformat Code (Option + Command + L on macOS) and gofmt will be executed after internal GoLand formatter.

Czechoslovakia answered 6/9, 2021 at 14:11 Comment(0)
M
4

Now Goland has a section that will reformat your code on file save without setting up File Watchers:

  • Under Settings -> Tools -> Actions On Save you can activate the first entry

enter image description here

Murvyn answered 16/11, 2021 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.