Skip to main content
โšก Calmops

IDE Setup: VS Code for Go

IDE Setup: VS Code for Go

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.

Installation

Installing VS Code

  1. Download from code.visualstudio.com
  2. Install for your operating system
  3. Launch VS Code

Installing Go Extension

  1. Open VS Code
  2. Go to Extensions (Ctrl+Shift+X / Cmd+Shift+X)
  3. Search for “Go”
  4. 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

  1. 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

  1. Set breakpoints by clicking on line numbers
  2. Press F5 or go to Run โ†’ Start Debugging
  3. Use debug console to inspect variables
  4. 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

  1. Open Test Explorer (left sidebar)
  2. View all tests in your project
  3. Run individual tests or test files
  4. View test results and coverage

Coverage

{
    "go.coverOnSingleTest": true,
    "go.coverOnSingleTestFile": true
}

Then:

  1. Run a test
  2. Coverage will be displayed with highlighting
  3. 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

Essential Extensions

  1. Go (golang.go) - Official Go extension
  2. Code Runner (formulahendry.code-runner) - Run code snippets
  3. REST Client (humao.rest-client) - Test APIs
  4. Thunder Client (rangav.vscode-thunder-client) - API testing

Productivity Extensions

  1. GitLens (eamodio.gitlens) - Git integration
  2. GitHub Copilot (github.copilot) - AI code completion
  3. Prettier (esbenp.prettier-vscode) - Code formatting
  4. Error Lens (usernamehw.errorlens) - Inline error display

Theme Extensions

  1. One Dark Pro (zhuangtongfa.material-theme)
  2. Dracula Official (dracula-theme.theme-dracula)
  3. 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

  1. Use workspace settings - Configure per project
  2. Enable formatting on save - Keep code consistent
  3. Use golangci-lint - Comprehensive linting
  4. Set up debugging - Debug configurations ready
  5. Use test explorer - Easy test management
  6. Enable coverage - Visualize test coverage
  7. Keep tools updated - Run go install regularly

โŒ 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

Tools and Extensions

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