golang channel是什么

发布时间:2022-05-15 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了golang channel是什么脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

Go语言中的channel是实现goroutine间无锁通信的关键机制,他使得写多线程并发程序变得简单、灵活、触手可得。

Channel是Go中的一个核心类型,你可以把它看成一个管道,通过它并发核心单元就可以发送或者接收数据进行通讯(communication)。 (推荐学习:go)

它的操作符是箭头 <- 。

ch <- v    // 发送值v到Channel ch中
v := <-ch  // 从Channel ch中接收数据,并将数据赋值给v

channel结构

tyPE hchan struct {
   qcount   uint           // total data in the queue 队列中存在的个数
   dataqsiz uint           // size of the circular queue buffer大小 实现看起来是个循环数组
   buf      unsafe.Pointer // points to an array of dataqsiz elements 数组指针
   elemsize uint16       //channel类型的大小
   closed   uint32      //channel是否关闭
   elemtype *_type // element type //channel 类型
   sendx    uint   // send index  //发送index
   recvx    uint   // receive index //接收index
   recvq    waitq  // list of recv waITers //接收链表 即读channel的goroutine
   sendq    waitq  // list of send waiters //发送链表 即写channel的goroutine

   // lock PRotects all fields in hchan, as well as several
   // fields in sudogs blocked on this channel.
   //
   // Do not change another G&#39;s status while holding this lock
   // (in particular, do not ready a G), as this can deadlock
   // with stack shrinking.
   lock mutex
}

以上就是golang channel是什么的详细内容,更多请关注脚本宝典其它相关文章

脚本宝典总结

以上是脚本宝典为你收集整理的golang channel是什么全部内容,希望文章能够帮你解决golang channel是什么所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: