Channels
channel 通过传递特定元素类型的值,提供了一套两个 goroutines 同步执行及交流的机制。 <- 操作符制定 channel 发送或接收的方向。如果没有明确方向,则通道为双向的。
chan Sushi // can be used to send and receive values of type Sushi
chan<- float64 // can only be used to send float64s
<-chan int // can only be used to receive ints
Channel 为引用类型,使用 make 分配。
ic := make(chan int) // unbuffered channel of ints
wc := make(chan *Work, 10) // buffered channel of pointers to Work
要向通道传递值,以二进制操作符的方式使用 <- 。要接收数据,则以一元运算符的方式使用它。
ic <- 3 // Send 3 on the channel.
work := <-wc // Receive a pointer to Work from the channel.
如若 channel 无缓冲区,则发送者堵塞,直到接收者接受到传值。如果 channel 有缓冲区,发送者堵塞,直到接收者开始读取缓冲区;如果缓冲区满了,则需要等到某些接收者开始检索值。接收者堵塞,直到有数据可接收。
close 函数将记录通道不能再被用来发送数据。当调用 close 函数后,在所有先前发送的值都被接收以后,接收操作将不会堵塞,同时返回 0 值。一个多值的接收操作将能够获取到 channel 是否被关闭的指示。
ch := make(chan string)
go func() {
ch <- "Hello!"
close(ch)
}()
fmt.Println(<-ch) // Print "Hello!".
fmt.Println(<-ch) // Print the zero value "" without blocking.
fmt.Println(<-ch) // Once again print "".
v, ok := <-ch // v is "", ok is false.
在下面的例子,我们将使 Publish 函数返回一个通道,它将被用来在文本发表完成后广播消息
// Publish prints text to stdout after the given time has expired.
// It closes the wait channel when the text has been published.
func Publish(text string, delay time.Duration) (wait <-chan struct{}) {
ch := make(chan struct{})
go func() {
time.Sleep(delay)
fmt.Println(text)
close(ch)
}()
return ch
}
下面就是 Publish 函数的大概用法
wait := Publish("important news", 2 * time.Minute)
// Do some more work.
<-wait // blocks until the text has been published