Go 语言开发工具
GoLand、VS Code、命令行工具链的取舍与常用命令。
Key PointsGoLand 开箱即用但要钱;VS Code + gopls 免费且够用;命令行工具链是共同基础。
三选一
| Tool | Price | Strength |
|---|---|---|
| GoLand | 付费 | 重构、调试、数据库工具最完整 |
| VS Code + Go | 免费 | 轻量,gopls 就是官方语言服务器 |
| Vim / Neovim | 免费 | 终端内工作流,配 gopls 一样有补全 |
| LiteIDE | 免费 | 老牌 Go 专用 IDE,现已少用 |
命令行工具链
Bash
go version # 版本
go env # 全部环境变量
go mod init <path> # 初始化模块
go mod tidy # 增删依赖到刚好
go get <pkg>@<ver> # 装依赖
go list -m all # 列出依赖树
go run . # 编译并运行
go build -o app . # 编译
go install ./cmd/x # 装到 $GOPATH/bin
go test ./... # 测试
go test -race ./... # 竞态检测
go vet ./... # 官方静态检查
go doc fmt.Println # 看文档
go fmt ./... # 格式化
go clean -cache # 清构建缓存代码质量工具
Bash
# 静态检查(比 go vet 严格得多)
go install honnef.co/go/tools/cmd/staticcheck@latest
staticcheck ./...
# 漏洞扫描
go install golang.org/x/vuln/cmd/govulncheck@latest
govulncheck ./...
# 基准测试
go test -bench=. -benchmem
# 覆盖率
go test -coverprofile=cover.out ./...
go tool cover -html=cover.outOutput
$ staticcheck ./...
main.go:12:2: this value of err is never used (SA4006)
$ go test -bench=. -benchmem
BenchmarkParse-8 1243156 964.2 ns/op 512 B/op 8 allocs/op性能分析
Go
import _ "net/http/pprof"
// 在服务里开一个端口
go func() {
http.ListenAndServe("localhost:6060", nil)
}()Bash
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
go tool pprof http://localhost:6060/debug/pprof/heapOutput
(pprof) top5
Showing nodes accounting for 1.82s, 82.7% of 2.20s total
flat flat% sum% cum cum%
0.72s 32.7% 32.7% 1.24s 56.4% runtime.mapassign_faststr
0.41s 18.6% 51.4% 0.41s 18.6% runtime.memmovenet/http/pprof is Go's most underrated feature: expose CPU, heap, goroutine and block profiles from a live service and view flame graphs with go tool pprof. Other languages need an agent.
Never expose the
pprof port publicly. It has no auth, so anyone can pull heap snapshots (possibly sensitive) and trigger CPU sampling. Bind to localhost or an internal port.调试器
Bash
go install github.com/go-delve/delve/cmd/dlv@latest
dlv debug # 编译并进入调试
dlv test # 调试测试
dlv attach <pid> # 附加到运行中的进程Output
(dlv) break main.main
Breakpoint 1 set at 0x10d5c60 for main.main() ./main.go:5
(dlv) continue
> main.main() ./main.go:5 (hits goroutine(1):1 total:1)
(dlv) print x
1dlv is the official debugger and the engine behind VS Code and GoLand. Once you can use it, most fmt.Println debugging disappears.
三选一
| 工具 | 价格 | 优势 |
|---|---|---|
| GoLand | 付费 | 重构、调试、数据库工具最完整 |
| VS Code + Go | 免费 | 轻量,gopls 就是官方语言服务器 |
| Vim / Neovim | 免费 | 终端内工作流,配 gopls 一样有补全 |
| LiteIDE | 免费 | 老牌 Go 专用 IDE,现已少用 |
命令行工具链
Bash
go version # 版本
go env # 全部环境变量
go mod init <path> # 初始化模块
go mod tidy # 增删依赖到刚好
go get <pkg>@<ver> # 装依赖
go list -m all # 列出依赖树
go run . # 编译并运行
go build -o app . # 编译
go install ./cmd/x # 装到 $GOPATH/bin
go test ./... # 测试
go test -race ./... # 竞态检测
go vet ./... # 官方静态检查
go doc fmt.Println # 看文档
go fmt ./... # 格式化
go clean -cache # 清构建缓存代码质量工具
Bash
# 静态检查(比 go vet 严格得多)
go install honnef.co/go/tools/cmd/staticcheck@latest
staticcheck ./...
# 漏洞扫描
go install golang.org/x/vuln/cmd/govulncheck@latest
govulncheck ./...
# 基准测试
go test -bench=. -benchmem
# 覆盖率
go test -coverprofile=cover.out ./...
go tool cover -html=cover.outOutput
$ staticcheck ./...
main.go:12:2: this value of err is never used (SA4006)
$ go test -bench=. -benchmem
BenchmarkParse-8 1243156 964.2 ns/op 512 B/op 8 allocs/op性能分析
Go
import _ "net/http/pprof"
// 在服务里开一个端口
go func() {
http.ListenAndServe("localhost:6060", nil)
}()Bash
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
go tool pprof http://localhost:6060/debug/pprof/heapOutput
(pprof) top5
Showing nodes accounting for 1.82s, 82.7% of 2.20s total
flat flat% sum% cum cum%
0.72s 32.7% 32.7% 1.24s 56.4% runtime.mapassign_faststr
0.41s 18.6% 51.4% 0.41s 18.6% runtime.memmovenet/http/pprof 是 Go 最被低估的能力:线上服务直接暴露 CPU、堆、goroutine、block 四类 profile,用 go tool pprof 看火焰图。别的语言要额外装 agent。
pprof 端口不要暴露在公网。它没有鉴权,任何人都能拉取堆快照(可能含敏感数据)并触发高 CPU 采样。只绑 localhost 或用内网端口。调试器
Bash
go install github.com/go-delve/delve/cmd/dlv@latest
dlv debug # 编译并进入调试
dlv test # 调试测试
dlv attach <pid> # 附加到运行中的进程Output
(dlv) break main.main
Breakpoint 1 set at 0x10d5c60 for main.main() ./main.go:5
(dlv) continue
> main.main() ./main.go:5 (hits goroutine(1):1 total:1)
(dlv) print x
1dlv 是 Go 的官方调试器,VS Code 和 GoLand 底层都用它。会用它之后,加 fmt.Println 调试的需求会少一大半。