Hopp til hovedinnhold

HansPetter.no :blog

Configuring VS Code with Prettier and Go Lang Formatting


I faced a challenge trying to configure Visual Studio Code (VS Code) to format Go files without interfering with Prettier, my default formatter for everything else. Here’s how I solved it.

Setting Up VS Code for Go Formatting

I followed this Boot.dev guide, but I ran into issues because Prettier was overriding my Go settings. To resolve this, I updated my settings.json to specify the Go formatter explicitly for Go files:

"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"go.formatTool": "goimports",
"[go]": {
	"editor.defaultFormatter": "golang.go"
},
"[go.mod]": {
	"editor.defaultFormatter": "golang.go"
},

(Only the relevant lines are shown)

Useful Extras

Handling source.organizeImports Warnings

When adding the suggested config from Boot.dev, I noticed a warning related to source.organizeImports set to true. Since this now defaults to "explicit", I removed that line to avoid the warning.

Installing the Go Language Extension

To get Go support in VS Code, I installed the Go language extension. Here’s how I did it: