Golang 常用命令
run
go run main.go
-mod
选项,go help modules
查看详细,-mod
在其他部分命令中也可指定,具体是 build, clean, get, install, list, run, and test
- vendor
- mod
- readonly
test
测试用例要求
- 文件名须以”_test.go"结尾
- 函数名须以"Test"打头,并且形参为 (t *testing.T)
运行命令
// -v 打印 log
go test -v xlsx_test.go xlsx.go
go test ./...
参数
// 直接查看覆盖率
go test -cover
// 输出统计信息文件
go test -coverprofile=profile.out
// 也可以定义cover模式
go test -covermode=count -coverprofile=profile.out
// 查看每个函数的覆盖率
go tool cover -func=profile.out
// 在浏览器详细查看每行的测试覆盖情况
go tool cover -html=profile.out
-covermode
选项
- set: 每个语句是否执行?(默认)
- count: 每个语句执行了几次
- atomic: 类似于 count, 但表示的是并行程序中的精确计数
pprof 和火焰图
生成 profile 文件的两种方式
通过 pprof
go tool pprof --seconds 25 http://localhost/debug/pprof/profile
通过 bench test
go test -bench=".*" -cpuprofile=cpu.profile ./
生成文件后,查看分析结果
go tool pprof -http=:8081 cpu.profile
也可以直接通过 web 界面查看
go tool pprof -http=:6060 http://127.0.0.1:8080/debug/pprof/profile
env
设置私有仓库 go1.13 以上有效
go env -w GOPRIVATE="*.luojilab.com"
go 1.13 以下
git config --global url."git@xxx.luojilab.com:".insteadOf "https://xxx.luojilab.com/"
参考
build
CGO_ENABLED=0 GOOS=linux go build -mod vendor -a -ldflags "-extldflags '-f'" -o main