← Back to Go Index

VS Code Go gopls Organize Imports

source.organizeImports, editor.defaultFormatter, and missing import troubleshooting

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

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.