Auto format/indent of code


Hi all

So I've been a long time Pycharm user. for Go code I've been using VSCode, but decided over the weekend to subscribe/buy a license for Goland and switch back to a Jetbrains IDE, I like them... :)

But in VSCode, as I typed my code, it auto indented/formated the code.

how do I accomplish the same for Goland,

As GoLand is a Jetbrains product specific for Go code, I'd expect minimum plugin's required for basic functionality... vs a VSCode which is generic for multiple languages, which you then customise via a plugin for your specific language.

Please help.


Official comment

Hi George, GoLand should indent your code while you type. If it's not the case for you, it'd be great if you can share an example when it doesn't work. As to code formatting, by default, the IDE expects an explicit user action (Code | Reformat Code or Tools | Go Tools | Go Fmt File). If you want it to run automatically, you can add `gofmt` under Settings | Tools | Files Watchers. Please let me know if it helps.

Hi there

I downloaded and installed Goland, note I did have it installed previously, but don't recall changing any settings.

it's def not auto formating/indenting the code, at all.

Example: the below example I have to indent the types to align ... thinking this might fall more under code formatting, something I would expect/would like to happen when ever I execute save. is it maybe possible to tie a gofmt to a save action.

type tp_general struct {
hostname string
debuglevel int
sdfs string

Hey George, there are several ways to configure auto-formatting on save. As Artem said, you can configure gofmt on save via File Watchers:

  • Navigate to Settings / Preferences | Tools | File Watchers.
  • Click on the plus icon and select go fmt.
  • Expand Advanced Options and select Auto-save edited files to trigger the watcher or Trigger the watcher on external changes if needed.
  • Apply changes, go to the editor and edit piece of code, then press CTRL(Command)+S.

If you don't want to configure the watcher for every project, there is a small trick:

  • Navigate to File | New Project Settings | Preferences / Settings for New Projects.
  • Select Tools | File Watchers and configure the same steps as before.

After that, all new projects should contain gofmt watcher by default. There is a small gif of how it works.

Besides File Watchers, you can use the 3rd-party plugin Save Actions. It's also pretty easy to configure and use (Preferences / Settings | Save Actions).

Thanks for your feedback. We currently investigate what our users prefer to use (GO-10404). If you're interested in why gofmt on save isn't enabled by default yet and possible action points, you can read the article Running ‘go fmt’ on Save in our blog.

We keep an eye on it.

awesome, done, works. like :)
thank you.

Please sign in to leave a comment.