Direct answer: recommended settings.json
When the VS Code Go extension uses gopls, set the official extension as the Go formatter and run source.organizeImports on save:
{
"[go]": {
"editor.defaultFormatter": "golang.go",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
}
},
"go.useLanguageServer": true
}
Key point
editor.defaultFormatter: "golang.go" selects the official Go extension. source.organizeImports calls gopls import organization, which removes unused imports and can add missing imports when the symbol resolves in the module context.
What gopls organizeimports does
- Groups and sorts imports according to Go toolchain conventions.
- Removes unused imports so the Go compiler does not fail.
- Adds missing imports when identifiers can be uniquely resolved from module dependencies.
- Does not guess unknown packages; broken modules, missing dependencies, or opening the wrong folder can prevent auto-add.
Troubleshooting missing imports
1. Open the module root
Open the directory that contains go.mod, not a single file detached from the module.
2. Check go.mod
Run go mod tidy and confirm dependencies download without module path errors.
3. Update gopls
Run Go: Install/Update Tools in the command palette and select gopls.
4. Remove formatter conflicts
If multiple Go formatters are installed, keep golang.go as the default formatter.
Related chapters
Continue with Go environment setup and VS Code configuration, or review Go tooling and ecosystem.