Visual Studio Code has become the most popular editor for Go development. With the right extensions and configuration, VS Code provides a powerful, lightweight development environment. This guide walks you through setting up VS Code for professional Go development. For more context, see Go Installation Guide, Go Ecosystem Overview, Go Best Practices.
Installation
Installing VS Code
- Download from code.visualstudio.com
- Install for your operating system
- Launch VS Code
Installing Go Extension
- Open VS Code
- Go to Extensions (Ctrl+Shift+X / Cmd+Shift+X)
- Search for “Go”
- Install the official Go extension by Go Team at Google
The extension provides:
- IntelliSense and code completion
- Debugging support
- Testing integration
- Linting and formatting
- Code navigation
Essential Configuration
Basic Settings
Create or edit .vscode/settings.json in your workspace:
{
"[go]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
},
"editor.defaultFormatter": "golang.go"
},
"go.lintOnSave": "package",
"go.lintTool": "golangci-lint",
"go.lintFlags": ["--fast"],
"go.useLanguageServer": true,
"go.languageServerFlags": ["-rpc.trace"],
"go.coverOnSingleTest": true,
"go.coverOnSingleTestFile": true
}
Formatting and Linting
{
"go.formatTool": "goimports",
"go.formatOnSave": true,
"go.lintOnSave": "package",
"go.lintTool": "golangci-lint",
"go.lintFlags": [
"--fast",
"--deadline=5m"
]
}
Code Coverage
{
"go.coverOnSingleTest": true,
"go.coverOnSingleTestFile": true,
"go.coverageDecorator": {
"type": "gutter",
"coveredHighlightColor": "rgba(64,128,128,0.5)",
"uncoveredHighlightColor": "rgba(128,64,64,0.25)",
"coveredBorderColor": "rgba(64,128,128,0.8)",
"uncoveredBorderColor": "rgba(128,64,64,0.8)"
}
}
Installing Tools
The Go extension requires several tools for full functionality. When you first open a Go file, VS Code will prompt you to install missing tools.
Manual Installation
# gopls - Language server
go install github.com/golang/tools/gopls@latest
# golangci-lint - Linter
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
# goimports - Import formatter
go install golang.org/x/tools/cmd/goimports@latest
# godef - Go to definition
go install github.com/rogpeppe/godef@latest
# guru - Code analysis
go install golang.org/x/tools/cmd/guru@latest
# dlv - Debugger
go install github.com/go-delve/delve/cmd/dlv@latest
# staticcheck - Static analysis
go install honnef.co/go/tools/cmd/staticcheck@latest
Verifying Installation
# Check if tools are installed
which gopls
which golangci-lint
which dlv
# Or check in GOPATH
ls $GOPATH/bin/
Debugging
Setting Up Debugging
- Create
.vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Connect to Process",
"type": "go",
"request": "attach",
"mode": "local",
"processId": "${command:pickProcess}"
},
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${fileDirname}",
"env": {},
"args": []
},
{
"name": "Launch Test",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}",
"args": [
"-test.run",
"^TestMain$"
]
}
]
}
Debugging Basics
- Set breakpoints by clicking on line numbers
- Press F5 or go to Run → Start Debugging
- Use debug console to inspect variables
- Step through code with F10 (step over) or F11 (step into)
Debug Configuration Examples
Debug current file:
{
"name": "Debug Current File",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${fileDirname}",
"env": {},
"args": []
}
Debug with arguments:
{
"name": "Debug with Args",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/cmd/myapp",
"args": ["--config", "config.yaml", "--verbose"]
}
Debug tests:
{
"name": "Debug Test",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}",
"args": ["-test.run", "^TestMyFunction$"]
}
Testing Integration
Running Tests
- Run test under cursor: Ctrl+Shift+F10 (Cmd+Shift+F10 on Mac)
- Run all tests: Go to Test Explorer and click “Run All”
- Debug test: Right-click test and select “Debug Test”
Test Explorer
- Open Test Explorer (left sidebar)
- View all tests in your project
- Run individual tests or test files
- View test results and coverage
Coverage
{
"go.coverOnSingleTest": true,
"go.coverOnSingleTestFile": true
}
Then:
- Run a test
- Coverage will be displayed with highlighting
- Green = covered, red = uncovered
Code Navigation
Go to Definition
- Keyboard: F12 or Ctrl+Click
- Command: Go → Go to Definition
Find References
- Keyboard: Shift+F12
- Command: Go → Find All References
Rename Symbol
- Keyboard: F2
- Command: Go → Rename Symbol
Outline
- Keyboard: Ctrl+Shift+O
- View: Open Outline panel
Recommended Extensions
Essential Extensions
- Go (golang.go) - Official Go extension
- Code Runner (formulahendry.code-runner) - Run code snippets
- REST Client (humao.rest-client) - Test APIs
- Thunder Client (rangav.vscode-thunder-client) - API testing
Productivity Extensions
- GitLens (eamodio.gitlens) - Git integration
- GitHub Copilot (github.copilot) - AI code completion
- Prettier (esbenp.prettier-vscode) - Code formatting
- Error Lens (usernamehw.errorlens) - Inline error display
Theme Extensions
- One Dark Pro (zhuangtongfa.material-theme)
- Dracula Official (dracula-theme.theme-dracula)
- Nord (arcticicestudio.nord-visual-studio-code)
Keyboard Shortcuts
Essential Shortcuts
| Action | Windows/Linux | Mac |
|---|---|---|
| Format Document | Shift+Alt+F | Shift+Option+F |
| Go to Definition | F12 | F12 |
| Find References | Shift+F12 | Shift+F12 |
| Rename Symbol | F2 | F2 |
| Quick Fix | Ctrl+. | Cmd+. |
| Run Test | Ctrl+Shift+F10 | Cmd+Shift+F10 |
| Debug Test | Ctrl+Shift+F9 | Cmd+Shift+F9 |
| Toggle Breakpoint | F9 | F9 |
| Continue | F5 | F5 |
| Step Over | F10 | F10 |
| Step Into | F11 | F11 |
Workspace Settings
Project-Specific Settings
Create .vscode/settings.json in your project:
{
"[go]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
}
},
"go.lintOnSave": "package",
"go.lintTool": "golangci-lint",
"go.lintFlags": ["--fast"],
"go.useLanguageServer": true,
"go.coverOnSingleTest": true,
"editor.rulers": [80, 120],
"editor.tabSize": 4,
"editor.insertSpaces": false
}
User Settings
Edit user settings (File → Preferences → Settings):
{
"go.useLanguageServer": true,
"go.lintOnSave": "package",
"go.lintTool": "golangci-lint",
"editor.formatOnSave": true,
"editor.defaultFormatter": "golang.go"
}
Troubleshooting
gopls Not Working
# Reinstall gopls
go install github.com/golang/tools/gopls@latest
# Check gopls version
gopls version
# Restart VS Code
Linting Issues
# Install golangci-lint
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
# Run manually to test
golangci-lint run ./...
# Check configuration
cat .golangci.yml
Debugging Not Working
# Install dlv
go install github.com/go-delve/delve/cmd/dlv@latest
# Check dlv version
dlv version
# Restart VS Code and try again
IntelliSense Not Working
# Clear cache
rm -rf ~/.cache/go-build
# Restart language server
# Command Palette → Go: Restart Language Server
# Check language server logs
# Output panel → Go
Performance Tips
Optimize for Large Projects
{
"go.lintOnSave": "off",
"go.lintFlags": ["--fast"],
"go.useLanguageServer": true,
"go.languageServerFlags": ["-rpc.trace"],
"editor.formatOnSave": false
}
Exclude Directories
{
"files.exclude": {
"**/node_modules": true,
"**/.git": true,
"**/vendor": true
},
"search.exclude": {
"**/node_modules": true,
"**/vendor": true
}
}
Best Practices
✅ Good Practices
- Use workspace settings - Configure per project
- Enable formatting on save - Keep code consistent
- Use golangci-lint - Comprehensive linting
- Set up debugging - Debug configurations ready
- Use test explorer - Easy test management
- Enable coverage - Visualize test coverage
- Keep tools updated - Run
go installregularly
❌ Anti-Patterns
// ❌ Bad: Disabling all linting
"go.lintOnSave": "off"
// ❌ Bad: Not using formatting
// Manual formatting is error-prone
// ❌ Bad: Ignoring test failures
// Always fix failing tests
// ❌ Bad: Not using version control
// Always commit .vscode/settings.json
Resources and References
Official Resources
- VS Code Go Extension - Official extension
- VS Code Go Wiki - Comprehensive guide
- gopls Documentation - Language server docs
Recommended Reading
- VS Code Go Getting Started - Quick start guide
- Debugging Go Code - Debugging guide
- Go Tools Documentation - Official Go tools
Tools and Extensions
- golangci-lint - Comprehensive linter
- goimports - Import formatter
- Delve Debugger - Go debugger
Summary
VS Code is an excellent choice for Go development:
- Install the official Go extension
- Configure formatting and linting
- Set up debugging configurations
- Use test explorer for testing
- Leverage keyboard shortcuts
- Keep tools updated
- Customize for your workflow
With proper setup, VS Code provides a productive, efficient Go development environment.
Comments