Channels Internals
Learn about the hchan struct and some deep concepts in Golang.
We'll cover the following...
We'll cover the following...
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 queuedataqsiz uint // size of the circular queuebuf unsafe.Pointer // points to an array of dataqsiz elementselemsize uint16closed uint32elemtype *_type // element typesendx uint // send indexrecvx uint // receive indexrecvq waitq // list of recv waiterssendq 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
: