Go Example--定时器

时间:2021-01-16 14:35:49
package main

import (
"fmt"
"time"
) func main() {
//定时器2s
timer1 := time.NewTimer(time.Second * 2)
//读取通道,阻塞2s
<-timer1.C fmt.Println("Timer 1 expired") timer2 := time.NewTimer(time.Second)
go func() {
<-timer2.C
fmt.Println("Timer 2 expired")
}()
//停止定时器
stop2 := timer2.Stop()
if stop2 {
fmt.Println("Timer 2 stoped")
}
}