liveBook · Manning

Buffered channel

As explained in Learn Concurrent Programming with Go

A buffered channel is a Go channel with finite storage capacity that permits sends or receives to proceed without an immediate counterpart while capacity or queued values remain.

A buffered channel is a Go channel with finite message storage. A sender continues without an immediate receiver while capacity remains; a receiver continues while queued messages remain. Messages reach receivers in send order. section 7.1.2 section 7.4

Its behavior follows the buffer’s state:

  • An available slot lets a send proceed and queues the message. section 7.1.2
  • A full buffer blocks the sender until a receiver consumes a message. section 7.1.2
  • A receiver drains queued messages even after sending has stopped. section 7.1.2
  • An empty buffer blocks the receiver until another message arrives. section 7.1.2

A fast producer and slow receiver

Create msgChannel with capacity three, start a receiver that sleeps for one second between reads, and send six integers followed by -1. The first messages enter the buffer quickly; later sends wait as the receiver frees space. The receiver prints 1 through 6, then -1, while a wait group coordinates completion.

How Learn Concurrent Programming with Go covers it

How deep it goes
Explains capacity, queue buildup, ordered delivery, and blocking, then relates implementation to queues, semaphores, and mutexes.
Examples
Demonstrates a capacity-three integer channel, six fast sends, a one-per-second receiver, and a -1 stopping value.
What it assumes
Assumes goroutines and basic channel communication while building on previously introduced concurrency primitives.
What you can do after
Operate/configure buffered channels by selecting capacity, observing queue length, coordinating receivers, and waiting for completion.
How it connects
Connects buffered channels with goroutines, message passing, queues, semaphores, mutexes, and wait groups.

Also explained in

Compare how 3 books explain buffered channel →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Concurrent Programming with Go. The section links point to the parts of the book it draws on.