How to set vscode format golang code on save?
Solution 1
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.
Solution 2
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 }
Solution 3
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 and you should see the go version. If you see an Exclamation icon click on it and install the suggested extensions.
Nick
Updated on November 28, 2021Comments
-
Nick over 2 years
I'm using
vscode
withGo
extensions to editgolang
source code. Each time I want to format code, I have to pressCtrl-Shift-i
onlinux
,Shift-Alt-F
onWindows
, orCtrl-Shift-p
and typeformat
. Is it possible to set format on save, that is, when I pressctrl-s
, it format the code automatically usinggo.fmt
(or something alike)?