Home > Backend Development > Golang > what is golang channel

what is golang channel

(*-*)浩
Release: 2019-12-03 09:29:02
Original
2767 people have browsed it

what is golang channel

The channel in the Go language is the key mechanism to achieve lock-free communication between goroutines. It makes writing multi-threaded concurrent programs simple, flexible and accessible.

Channel is a core type in Go. You can think of it as a pipe, through which the concurrent core unit can send or receive data for communication. (Recommended learning: go)

Its operator is the arrow <-.

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

channel structure

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
}
Copy after login

The above is the detailed content of what is golang channel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template