Go 开发工具

VS Code + Go 插件:补全、跳转、调试、格式化、静态检查。

Beginner
Key Points装官方 Go 扩展,它会引导你装齐 `gopls`、`dlv` 等工具,之后补全和断点调试都能用。

装扩展

Search Go in the marketplace (publisher golang.go). Open any .go file and the status bar offers Install All to fetch the missing tools at once.

Bash
# 手动装(网络不好时逐个装更稳)
go install golang.org/x/tools/gopls@latest          # 语言服务器
go install github.com/go-delve/delve/cmd/dlv@latest # 调试器
go install golang.org/x/tools/cmd/goimports@latest  # 自动 import
go install honnef.co/go/tools/cmd/staticcheck@latest
Output
$ gopls version
golang.org/x/tools/gopls v0.16.2

值得开的设置

JSON
{
  "go.useLanguageServer": true,
  "go.formatTool": "goimports",
  "go.lintTool": "staticcheck",
  "editor.formatOnSave": true,
  "go.toolsManagement.autoUpdate": true,
  "[go]": {
    "editor.insertSpaces": false,
    "editor.codeActionsOnSave": { "source.organizeImports": "explicit" }
  }
}
SettingEffect
formatTool: goimports格式化同时自动补/删 import
lintTool: staticcheck比 go vet 严格得多
insertSpaces: falseGo 用 Tab 缩进(官方规范)

断点调试

JSON
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "调试当前包",
      "type": "go",
      "request": "launch",
      "mode": "auto",
      "program": "${fileDirname}"
    },
    {
      "name": "调试测试",
      "type": "go",
      "request": "launch",
      "mode": "test",
      "program": "${fileDirname}"
    }
  ]
}

Click left of a line number, press F5. Delve supports conditional breakpoints: right-click the dot → Edit Breakpoint → i == 5.

常用命令

Bash
go run .              # 编译并运行
go build -o app .     # 只编译
go test ./...         # 跑全部测试
go test -race ./...   # 带竞态检测
go vet ./...          # 官方静态检查
go mod tidy           # 整理依赖
go doc fmt.Println     # 看文档
go test -race is near-mandatory. A concurrency bug may hide for a hundred runs, then appear immediately under -race — at 5–10× the runtime.

Friends & Partners