在Ubuntu上编译Golang程序后,可通过以下方式测试:
_test.go结尾),使用testing包编写测试函数(以Test开头),通过go test命令运行。// math_test.go
func TestAdd(t *testing.T) {
if result := Add(2, 3); result != 5 {
t.Errorf("Add(2, 3) = %d; want 5", result)
}
}
go test(运行当前包所有测试)。go test -v。go test -cover,生成HTML报告:go test -coverprofile=coverage.out && go tool cover -html=coverage.out。Benchmark开头的函数,使用go test -bench=.运行。func BenchmarkAdd(b *testing.B) {
for i := 0; i < b.N; i++ {
Add(1, 2)
}
}
dlv debug启动调试会话,支持断点、单步执行等。参考资料: