定义

通道是go语言的一种数据类型,是goroutine之间的通信机制。

初始化
var c chan TYPE
ch := make(chan int)  //无缓冲通道
ch := make(chan string ,10) //有缓冲通道
ch := make(chan<- string) //只能用于接收的通道 单向通道
ch := make(<-chan float64) //只能用于发送的通道 单项通道
接收元素

从一个未初始化的通道中获取数据会永远阻塞。
从一个已关闭的通道中获取数据,如果通道中还有值可以正常获取,如果通道中没有值,会得到该通道类型的零值。
从一个没有值的通道中获取数据会被阻塞,直到通道被写入数据或者关闭。

strChan := make(chan string,3)
elem := <-strChan
elem ,ok := <-strChan
发送元素

向一个未初始化的通道中写入数据会永远阻塞。
向一个无缓冲的通道中写入数据会被终阻塞,直到该通道有至少一个接收操作进行为止,发送操作才能被唤醒。
向一个已关闭的通道写入数据会引起恐慌。所以不要在接收方去关闭一个通道。
向一个已满的通道写入数据也会被阻塞,直到有接收操作完成才能被唤醒。

strChan := make(chan string,3)
strChan<-"hello"
strChan<-"world"
用法
for与channel

for的range子句可以对通道进行读取操作

var ch = make(chan int)
for e:= range ch{
   fmt.Printf("element:%d",e)
}

for语句可以把通道已有的数据按顺序读出。当通道没有数据时,for语句会被阻塞在range子句处。

select与channel

select语句是一种仅能用于通道发送和接收的语句。一条select语句执行时会选择其中的一个分支并执行。

var intChan = make(chan int,10)
var strChan = make(chan string,10)
select{
case e:=<-intChan:
    fmt.Printf("this case wad selected,element = %v",e)
case e:=<-strChan:
    fmt.Printf("this case wad selected,element = %v",e)
 default:
    fmt.Println("default")

如果同时又多个case可以被触发,select会通过伪随机的算法随机选择一个case并执行。

timer与channel
定时器

time.Timer是time包提供的定时器。它可以在到达指定时间后发送一个元素到字段C中

timer := time.NewTimer(time.Second * 10)
fmt.Printf("now:%v",time.Now)
expiration := <-timer.C
fmt.Printf("now:%v",time.Now)

timer.C在10s之后才会有元素输出,expiration定义语句一直被阻塞,直到10s后,才能打印第二条语句。

expiration := <-time.After(time.Second * 10)

与timer是等价了,简化了timer的定义。

断续器

time.Tricker是time包提供的断续器,它能按指定时间间隔不断重复的发送元素到字段通道C中,当下一次间隔时间点到来之时,通道中还能没被取出的元素,则当前本次操作就会被立即取消。

var ch = make(chan int 3)
ticker := time.NewTricker(time.Second )
for _ = range ticker.C{
    select {
    case ch <- 1:
    case ch <- 2:
    case ch <- 3:
    }
 }
特性
并发安全

channel的数据结构如下:

type hchan struct {
    qcount   uint           // 数组长度,即已有元素个数
    dataqsiz uint           // 数组容量,即可容纳元素个数
    buf      unsafe.Pointer // 数组地址
    elemsize uint16         // 元素大小
    closed   uint32
    elemtype *_type // 元素类型
    sendx    uint   // 下一次写下标位置
    recvx    uint   // 下一次读下标位置
    recvq    waitq  // 读等待队列
    sendq    waitq  // 写等待队列
    lock     mutex
}

lock保证了chan的多进程之间的并发安全。

同步与异步

无缓冲通道同步
有缓冲通道异步