Skip to main content

One post tagged with "vscode"

View All Tags

VS Code Setup for Golang Programming

· 5 min read
Dipjyoti Metia
Chapter Lead - Testing

As a Go developer, having the right programming environment can significantly boost your productivity. Visual Studio Code has become the go-to editor for many Golang developers, thanks to its excellent extension ecosystem and customizability. In this guide, I'll walk you through creating the perfect VS Code setup for Go development.

Table of Contents

  1. Installing Prerequisites
  2. Essential Extensions
  3. Advanced Configuration
  4. Productivity Tips and Tricks
  5. Debugging Setup
  6. Theme and UI Customization

Installing Prerequisites

Before we dive into VS Code configuration, ensure you have:

  1. The latest version of Go installed (1.22+ recommended)
  2. Git for version control
  3. Visual Studio Code
  4. Make (optional but recommended for build automation)

Essential Extensions

Let's start with the must-have extensions for Go development:

  1. Go (ms-vscode.go)

    • Official Go extension
    • Provides language support, debugging, and testing features
    • Install by pressing Cmd+P (Mac) or Ctrl+P (Windows/Linux) and typing: ext install ms-vscode.go
  2. Go Test Explorer

    • Visual test runner for Go
    • Makes testing more intuitive and visual
  3. Error Lens

    • Inline error highlighting
    • Better error visibility without hovering
  4. GitLens

    • Enhanced Git integration
    • Great for team collaboration

Advanced Configuration

Here's an optimized settings.json configuration for Go development:

settings.json
{
"go.toolsManagement.autoUpdate": true,
"go.useLanguageServer": true,
"go.addTags": {
"tags": "json",
"options": "json=omitempty",
"promptForTags": false,
"transform": "snakecase",
},
"gopls": {
"formatting.gofumpt": true,
"usePlaceholders": true,
"ui.semanticTokens": true,
"staticcheck": false, // Enable if it's now better optimized
},
"go.lintTool": "golangci-lint",
"go.lintFlags": [
"--fast",
"--timeout",
"5m",
"--fix"
],
// disable test caching, race and show coverage (in sync with makefile)
"go.testFlags": [
"-cover",
"-race",
"-count=1",
"-v",
"-s",
"-benchtime=5s",
"-timeout=5m"
],
"go.enableCodeLens": {
"runtest": true,
},
// Go-specific editor settings
"[go]": {
"editor.insertSpaces": false,
"editor.formatOnSave": true,
"editor.formatOnSaveMode": "file",
"editor.stickyScroll.enabled": true, // Better navigation for long files
"editor.codeActionsOnSave": {
"source.organizeImports": "always",
"source.fixAll": "always"
},
},
// Enhanced inlay hints
"go.inlayHints.compositeLiteralFields": true,
"go.inlayHints.compositeLiteralTypes": true,
"go.inlayHints.functionTypeParameters": true,
"go.inlayHints.parameterNames": true,
"go.inlayHints.rangeVariableTypes": true,
"go.inlayHints.constantValues": true,
// Security checks
"go.diagnostic.vulncheck": "Imports",
"go.toolsEnvVars": {
"GOFLAGS": "-buildvcs=false" // Better performance for large repos
},
}

Productivity Tips and Tricks

1. Keyboard Shortcuts

Essential shortcuts for Go development:

  • F12: Go to definition
  • Alt+F12: Peek definition
  • Shift+Alt+F12: Find all references
  • F2: Rename symbol
  • Ctrl+Shift+P: Command palette (use for Go commands)

2. Snippets

Create custom snippets for common Go patterns. Here's an example snippet for a test function:

Test Function Snippet
{
"Test function": {
"prefix": "test",
"body": [
"func Test${1:Name}(t *testing.T) {",
" tests := []struct{",
" name string",
" input ${2:type}",
" want ${3:type}",
" }{",
" {",
" name: \"${4:test case}\",",
" input: ${5:value},",
" want: ${6:value},",
" },",
" }",
"",
" for _, tt := range tests {",
" t.Run(tt.name, func(t *testing.T) {",
" got := ${7:function}(tt.input)",
" if got != tt.want {",
" t.Errorf(\"got %v, want %v\", got, tt.want)",
" }",
" })",
" }",
"}"
],
"description": "Create a new test function with table-driven tests"
}
}

3. Task Automation

Create a tasks.json for common operations:

tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "go: build",
"type": "shell",
"command": "go build -v ./...",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "go: test",
"type": "shell",
"command": "go test -v -cover ./...",
"group": {
"kind": "test",
"isDefault": true
}
}
]
}

Debugging Setup

Configure launch.json for debugging:

launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
},
{
"name": "Attach to Process",
"type": "go",
"request": "attach",
"mode": "local",
"processId": "${command:pickProcess}"
}
]
}

Theme and UI Customization

For optimal Go development, I recommend:

  1. Theme: One Dark Pro or GitHub Theme
  2. Font: JetBrains Mono or Fira Code with ligatures
  3. Icon Theme: Material Icon Theme
  4. Editor: Line cursor, smooth blinking, and 4-space tabs
  5. Window: Zoom level 0.5 for better readability
  6. Rulers: Set at 88 and 120 columns for code alignment
  7. Minimap: Disable for better performance
  8. Inlay Hints: Font size 14 for better visibility
  9. Tree Indent: Set to 20 for better file navigation
  10. Auto Indent: Full for consistent code formatting

Apply these settings in your configuration:

settings.json
{
"window.zoomLevel": 0.5,
"workbench.iconTheme": "material-icon-theme",
"workbench.tree.indent": 20,
"editor.cursorStyle": "line",
"editor.cursorBlinking": "smooth",
"editor.fontSize": 14,
"editor.fontVariations": false,
"editor.inlayHints.fontSize": 14,
"editor.tabSize": 4,
"editor.insertSpaces": true,
"editor.autoIndent": "full",
"editor.fontFamily": "'Fira Code'",
"editor.fontLigatures": true,
"editor.minimap.enabled": false,
"editor.rulers": [88,120],
}

Conclusion

This setup provides a powerful, efficient, and visually appealing environment for Go development. Remember to regularly update your VS Code and extensions to get the latest features and improvements.

The beauty of VS Code lies in its customizability – feel free to modify these settings based on your preferences and workflow. The key is finding the right balance between functionality and simplicity that works best for you.

Happy coding! 🚀


Did you find this guide helpful? Follow me for more development tips and tricks!