Channels
Advanced · Concurrency
Beyond basic send/receive: buffering and direction.
- channels1 — buffered vs unbuffered (
make(chan T, n)) - channels2 — directional types (
chan<- Tsend-only,<-chan Treceive-only)
See also the concurrent, select, and concurrency_patterns topics.
Resources
Section titled “Resources”Exercises
Section titled “Exercises”channels1 test
Section titled “channels1 test”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))channels2 test
Section titled “channels2 test”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) }