go 工具链 (未完待续)
1. package
- go mod init maocaoying.com/learn
- go mod tidy
- go install // install cmd into GOPTAH
- go get // 感觉废弃了,直接go mod tidy
2. go help cmd
// help cmd detail info
3. test
- 
go test [-v] learn_test.go // 指定文件 默认所有testfunc 
- 
go test -v -run TestA select_test.go // TestA func. TestA* 正则匹配的都会执行 
- 
go test -v -bench=. benchmark_test.go 
- 
go test -v -bench=. -benchtime=5s benchmark_test.go 
- 
go test -v -bench=Alloc -benchmem benchmark_test.go 内存分配 
- 
go test -bench=. -run=none 命令得到以下结果 
 ![]() 
 因为默认情况下 go test 会运行单元测试,为了防止单元测试的输出影响我们查看基准测试的结果,可以使用-run=匹配一个从来没有的单元测试方法,过滤掉单元测试的输出,我们这里使用none,因为我们基本上不会创建这个名字的单元测试方法。
- 
-8 表示运行时对应的 GOMAXPROCS 的值; 
- 
7618 for range times =>b.N 
- 
146356 ns/op 每次操作花费 146356ns 
并行用法
func BenchmarkSprints(b *testing.B) {
   b.RunParallel(func(pb *testing.PB) {
     for pb.Next() {
       // do something
       fmt.Sprint("代码轶事")
     }
   })
 }
- RunParallel并发的执行benchmark。RunParallel创建p个goroutine然后把b.N个迭代测试分布到这些goroutine上。
- goroutine的数目默认是GOMAXPROCS。如果要增加non-CPU-bound的benchmark的并个数,在执行RunParallel之前那就使用b.SetParallelism(p int)来设置,最终goroutine个数就等于p * runtime.GOMAXPROCS(0)。
  // 重置计时器   在for循环之前用
  b.ResetTimer()
  // 停止计时器
  b.StopTimer()
  // 开始计时器
  b.StartTimer()
4. 调试 优化
- delve https://www.maocaoying.com/topic/971
- vet
- 竞态检测器 race
 $ go test -race mypkg
$ go run -race mysrc.go
$ go build -race mycmd
$ go install -race mypkg
