package main
import (
"fmt"
"time"
)
func pinger(c chan string) {
t := time.NewTicker(1 * time.Second)
for {
c <- "ping"
<-t.C
}
}
func main() {
messages := make(chan string)
go pinger(messages)
for {
msg := <-messages
fmt.Println(msg)
}
}
最近在看Go 语言入门经典,在12.3阻塞与流程控制这一小节时,关于<-t.C有疑惑,不知如何理解?请大家不吝赐教!!!
package time
import "errors"
// A Ticker holds a channel that delivers `ticks' of a clock
// at intervals.
type Ticker struct {
C <-chan Time // The channel on which the ticks are delivered.
r runtimeTimer
}
刚刚翻到time源码,声明了一个通道,原来如此,但是为什么这样使用<-t.C使用?没有接收者呀?
#1