我正在创建这个函数来测试创建和停止计时器。运行时出现死锁错误:
package main
import "fmt"
import "time"
func main() {
livenessTimer := &time.Timer{}
livenessInterval, _ := time.ParseDuration("1m")
for {
fmt.Print("Timer started")
livenessTimer = time.NewTimer(livenessInterval)
select {
case <-livenessTimer.C:
fmt.Print(time.Now())
fmt.Println("timer triggered")
}
if !livenessTimer.Stop() {
// drain timer from channel if any
fmt.Println("drain timer")
<-livenessTimer.C
}
}
}
当我运行此代码时,我收到此错误:
Timer started2009-11-10 23:01:00 +0000 UTC m=+60.000000001timer triggered
drain timer
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.main()
/tmp/sandbox748850751/prog.go:21 +0x2e0
Stop建议检查返回值并排空通道的文档。
holdtom
相关分类