这里我声明了一个推迟trace1的函数
func TestDeferFunc(t *testing.T) {
fmt.Println("start", time.Now())
defer trace1()
time.Sleep(3 * time.Second)
}
func trace1() {
startTime := time.Now()
fmt.Println("end time: ", startTime)
fmt.Println("execute time: ", time.Since(startTime))
}
运行后go test -run=^TestDeferFunc$,下面是我得到的
start 2019-11-26 12:50:59.59489797 +0800 CST m=+0.000202866
end time: 2019-11-26 12:51:02.595090951 +0800 CST m=+3.000395880
execute time: 49.065µs
然而,当我推迟另一个 annoymouse 函数时,事情发生了变化
func TestDeferFunc(t *testing.T) {
fmt.Println("start", time.Now())
defer trace2()()
time.Sleep(3 * time.Second)
}
func trace2() func() {
startTime := time.Now()
fmt.Println("end time: ", startTime)
fmt.Println("execute time: ", time.Since(startTime))
return func() {
fmt.Println("zzz")
}
}
下面是go test结果
start 2019-11-26 12:52:58.318472958 +0800 CST m=+0.000197852
end time: 2019-11-26 12:52:58.318554368 +0800 CST m=+0.000279262
execute time: 4.853µs
zzz
有人可以帮我吗!谢谢
三国纷争
哆啦的时光机
相关分类