Skip to content

Channels

Advanced · Concurrency

Beyond basic send/receive: buffering and direction.

  • channels1 — buffered vs unbuffered (make(chan T, n))
  • channels2 — directional types (chan<- T send-only, <-chan T receive-only)

See also the concurrent, select, and concurrency_patterns topics.

A buffered channel — make(chan T, n) — holds up to n values without a

Show hint
Give the channel a buffer so sends do not block:
ch := make(chan int, len(vals))

Source

Channel direction in a signature documents and enforces intent:

Show hint
Drain the receive-only channel with range:
for v := range in { got = append(got, v) }

Source