...

/

Channels Internals

Channels Internals

Learn about the hchan struct and some deep concepts in Golang.

The hchan struct

Internally, the channels implement an hchan struct, which holds the information related to the channels. It includes the buffer size buf, the sender sendx, the receiver recvx, the channel blocked due to sending sendq, the channel blocked due to receiving recvq, the mutex lock, and many more fields.

Press + to interact
type hchan struct {
qcount uint // total data in the queue
dataqsiz uint // size of the circular queue
buf unsafe.Pointer // points to an array of dataqsiz elements
elemsize uint16
closed uint32
elemtype *_type // element type
sendx uint // send index
recvx uint // receive index
recvq waitq // list of recv waiters
sendq waitq // list of send waiters
// lock protects all fields in hchan, as well as several
// fields in sudogs blocked on this channel.
lock mutex
}

Terminology

Let’s explore the terminology of the hchan struct.

  • buf: This represents the circular queue that stores the data.
  • closed:
...