1. 无缓冲通道 2. 有缓冲通道 有缓冲通道特点:当channel已经满,在向里面写数据就会阻塞,当channel已经为空,在从里面读数据就会阻塞. 3. 关闭channel package mainimport "fmt"func main() { c := make(chan int) go func() { for i := 0; i < 5; i++ { c <- i } // close可以关闭一个channel close(c) }() for { // 当c…
1. Channel是Go中的一个核心类型,你可以把它看成一个管道,通过它并发核心单元就可以发送或者接收数据进行通讯(communication). 2. select package main import ( "fmt" "time" ) func fibonacci(c, quit chan int) { x, y := 0, 1 for { select { case c <- x: // 负责往通道写数据 x, y = y, x + y time.S…
golang的channel实现位于src/runtime/chan.go文件.golang中的channel对应的结构是: // Invariants: // At least one of c.sendq and c.recvq is empty, // except for the case of an unbuffered channel with a single goroutine // blocked on it for both sending and receiving usi…