Go 开发工具
VS Code + Go 插件:补全、跳转、调试、格式化、静态检查。
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@latestOutput
$ 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" }
}
}| Setting | Effect |
|---|---|
formatTool: goimports | 格式化同时自动补/删 import |
lintTool: staticcheck | 比 go vet 严格得多 |
insertSpaces: false | Go 用 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.装扩展
在 VS Code 扩展市场搜 Go(发布者 golang.go),安装后打开任意 .go 文件,右下角会提示 Failed to find gopls,点 Install All 一次装齐。
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@latestOutput
$ 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" }
}
}| 设置 | 作用 |
|---|---|
formatTool: goimports | 格式化同时自动补/删 import |
lintTool: staticcheck | 比 go vet 严格得多 |
insertSpaces: false | Go 用 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}"
}
]
}在行号左侧点红点,F5 启动。Go 的调试器 dlv 支持条件断点:右键断点 → 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 几乎必开。并发 bug 在普通测试里可能一百次都不复现,加上 -race 一次就抓到,代价是慢 5~10 倍。