• 技术文章 >后端开发 >Golang

    golang channel是什么

    (*-*)浩(*-*)浩2019-12-03 09:29:02原创855

    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's status while holding this lock
       // (in particular, do not ready a G), as this can deadlock
       // with stack shrinking.
       lock mutex
    }

    以上就是golang channel是什么的详细内容,更多请关注php中文网其它相关文章!

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:golang
    上一篇:Golang Cgo是什么 下一篇:golang channel有什么好处
    大前端线上培训班

    相关文章推荐

    • 电商后台api使用golang还是python写好?• Golang 1.6: text/template内进行HTML/JavaScript/URL转义处理_html/css_WEB-ITnose• golang与php实现计算两个经纬度之间距离的方法• 使用Golang实现PHP的Addslashes和Stripslashes

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网