How to set vscode format golang code on save?
Asked Answered
A

6

43

I'm using vscode with Go extensions to edit golang source code. Each time I want to format code, I have to press Ctrl-Shift-i on linux, Shift-Alt-F on Windows, or Ctrl-Shift-p and type format. Is it possible to set format on save, that is, when I press ctrl-s, it format the code automatically using go.fmt (or something alike)?

Amazonas answered 23/2, 2016 at 7:12 Comment(0)
B
24

You should install this plugin: https://github.com/golang/vscode-go. One of the options is to set "auto format" on save: go.formatOnSave": false. It uses the Golang tooling for formatting.

Brisance answered 23/2, 2016 at 7:19 Comment(3)
Thank you very much. Actually I was using this plugin, but I didn't find the options to set before asking this question. ` "go.formatOnSave": true` did the trick. I think the default setting should be true instead of false.Amazonas
Another thing you should look at is changing "go.formatTool" from "go fmt" to "goimports" (and installing goimports). Having it manage your imports is very handy (though use with caution, double-check it's importing the right thing).Shameful
does't work for me, check the answer belowGallivant
B
47

For me, none of the answers worked. My Go version is 1.17.1, VSCode version is 1.60.1 and I'm using Linux Pop!_os.

After some digging online found this in the official VSCode documentation for Go. https://code.visualstudio.com/docs/languages/go#_formatting

My settings.json looks like this

"[go]": {
    "editor.insertSpaces": true,
    "editor.formatOnSave": true,
    "editor.defaultFormatter": "golang.go"
},

Note: You need to install the required extensions for the go lang in VS code. Check the bottom left bar after opening a *.go file and you should see the go version. If you see an exclamation icon then click on it and install the suggested extensions.

Baler answered 28/11, 2021 at 9:56 Comment(3)
"editor.defaultFormatter" was what I was missing - thanks!Kilovolt
This worked perfectly. Nice!Rabjohn
"editor.defaultFormatter" works like a charm! Thank you.Prostyle
U
26

From my visual code version, i cannot use config go.formatOnSave": false.

Then I can turn them off in settings as below:

  • Build (Turn off using go.buildOnSave setting)
  • Lint (Turn off using go.lintOnSave setting)
  • Vet (Turn off using go.vetOnSave setting)
  • Format (Turn off by adding the below in your settings):
    "[go]": {
        "editor.formatOnSave": false 
    }
    
Unselfish answered 15/5, 2019 at 3:2 Comment(2)
For Win: If you can not find it in VSCode then just go to %APPDATA%/Roaming/Code/User/settings.json and edit it manually. Works good since idk how to setup my own format for .go filesMacrocosm
"[go]": { "editor.formatOnSave": false } Adding the above to my usersettings.json is what fixed it for me. ThanksElo
B
24

You should install this plugin: https://github.com/golang/vscode-go. One of the options is to set "auto format" on save: go.formatOnSave": false. It uses the Golang tooling for formatting.

Brisance answered 23/2, 2016 at 7:19 Comment(3)
Thank you very much. Actually I was using this plugin, but I didn't find the options to set before asking this question. ` "go.formatOnSave": true` did the trick. I think the default setting should be true instead of false.Amazonas
Another thing you should look at is changing "go.formatTool" from "go fmt" to "goimports" (and installing goimports). Having it manage your imports is very handy (though use with caution, double-check it's importing the right thing).Shameful
does't work for me, check the answer belowGallivant
S
12

For me the followed settings worked. I disabled the annoying import refactoring.

"[go]": {
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": false
    },
}
Shortcake answered 15/3, 2022 at 13:25 Comment(1)
Thank you. This import organizer was so annoyingMonia
D
0

This is the settings that worked for me

"[go]": {
    "editor.formatOnSave": true,
    "editor.defaultFormatter": "golang.go",
    "editor.codeActionsOnSave": {
        "source.organizeImports": "always"
    }
}

Do not miss the "editor.defaultFormatter": "golang.go",

Darmstadt answered 3/7 at 15:18 Comment(0)
A
0

If you work with other languages and use Prettier, you can set golang.go for Golang and esbenp.prettier-vscode for anything else.

  "editor.formatOnSave": true,
  "[*]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[go]": {
    "editor.defaultFormatter": "golang.go"
  },
Adolphadolphe answered 14/7 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.