Go Channel
Contents
无缓冲通道
func main() {
c := make(chan int)
go func() {
time.Sleep(3 * time.Second)
fmt.Println("receive")
fmt.Println(<-c)
}()
c <- 1
fmt.Println("send")
}
执行结果。
receive
1
send
对于无缓冲通道,必须得send goroutine和receive goroutine双方都得准备好,如果两个 goroutine没有同时准备好,通道会导致先执行send或receive操作的goroutine阻塞等待。
这意味着执行c<-1时,发现没有receive方,于是阻塞等待,这就解释了为什么以上主程序执行到c<-1,然后阻塞,同时另一个goroutine三秒后打印,从channel中取值,最后主程序结束。
有缓冲通道
有缓冲通道有一个缓冲区,这个大小在声明时指定。所以他不要求goroutine同时完成send和receive,只有缓冲区满的时候,send操作才会阻塞,只有缓冲区空的时候,receive操作才会阻塞,试着为上面程序的通道加上缓冲区。
func main() {
c := make(chan int, 1)
go func() {
time.Sleep(3 * time.Second)
fmt.Println("receive")
fmt.Println(<-c)
}()
c <- 1
fmt.Println("send")
}
执行结果。
send