time.After可以用于在某个时间延迟之后执行某件事情,
package main
import "fmt"
import "time"
func main(){
for {
select {
case <- time.After(1000*time.Millisecond):
fmt.Println("hello")
}
}
}
每次调用time.After会生成新的定时器,并在duration之后写入chan,select中读取chan,实现延迟执行。 输出:
hello
hello
hello
hello
hello
hello
hello
hello
hello
......