標籤:源碼分析 time turned cond ati top garbage art collect
go語言--time.After()78873396
1.源碼分析:
// After waits for the duration to elapse and then sends the current time
// on the returned channel.
// It is equivalent to NewTimer(d).C.
// The underlying Timer is not recovered by the garbage collector
// until the timer fires. If efficiency is a concern, use NewTimer
// instead and call Timer.Stop if the timer is no longer needed.
func After(d Duration) <-chan Time {
return NewTimer(d).C
}
根據注釋可知,在等待給定的一段時間後,向傳回值發送目前時間,傳回值是一個單向唯讀通道
2.demo:
func main() { c := make(chan int, 1) select { case m := <-c: fmt.Println(m) case d := <-time.After(5 * time.Second): fmt.Println("time out") fmt.Println("current Time :", d) } }
time out
current Time : 2017-12-22 15:04:05.0462009 +0800 CST m=+5.004000001
3.分析:
第一個case中,通道c一直處於阻塞狀態;第二個case中,在time.After()結束後,從傳回值通道中取出一個值賦予了d,該值就是目前時間
go語言--time.After()