liveBook · Manning

Buffered channel

As explained in Learn Go with Pocket-Sized Projects

A buffered channel is a Go channel that can hold a bounded number of values without an immediately corresponding receive.

A buffered channel is a Go channel with a bounded queue. Create one with make, choosing its element type and capacity; sends proceed until that capacity is full. section 7.2.3

Use buffering to control when concurrent work blocks:

  • A send to a channel with available capacity stores the value without an immediately corresponding receive. section 7.2.3
  • A full buffer blocks the sending goroutine until a receiver removes a value; reads preserve first-in, first-out order. section 7.2.3
  • Choose capacity from the number of values written before reading starts, or benchmark it for the target workload and machine. section 7.5.2 section 9.7

Starting a concurrent maze solver

An unbuffered pathsToExplore channel blocks when Solve publishes the entrance before listening begins. Giving it a one-value buffer allows the initial path to be published, though the listener still needs a termination condition. A test instead uses make(chan *path, 3) so explore can publish three candidates while the test inspects the queue. section 9.3.4 section 9.3.6

c := make(chan int, 10) #1
c := make(chan int) #2

Compare buffered and unbuffered construction, then inspect send and receive syntax through the channel examples. section 7.2.3

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains construction, capacity, FIFO order, blocking, testing, and sizing tradeoffs, but defers deeper channel coordination patterns.
Examples
Uses completion notifications, concurrent cooking tasks, maze exploration, and a three-slot channel in unit tests.
What it assumes
Assumes familiarity with goroutines and basic Go syntax while teaching channel creation, sends, receives, closing, and blocking behavior.
What you can do after
Build it from scratch with make, select capacity, avoid blocked initial sends, and test queued concurrent results.
How it connects
Connects buffered channels with goroutines, unbuffered channels, channel closing, deadlocks, concurrency workloads, and performance testing.

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 Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.