midGolang
How do channels work for synchronization and communication?
Updated Apr 28, 2026
Short answer
Channels are typed conduits that allow goroutines to communicate and synchronize their execution.
Deep explanation
Channels act as pipes connecting concurrent goroutines. You can send values into channels from one goroutine and receive them in another using the <- operator. Unbuffered channels block the sender until a receiver is ready, ensuring strict synchronization. Buffered channels block only when full.
Real-world example
Using channels in a fan-out architecture where a master goroutine reads tasks from a DB and pushes them into a channel consumed by 10 worker goroutines.
Common mistakes
- Sending to a closed channel, which causes a panic. Receivers should check if the channel is closed, senders should dictate closing.
Follow-up questions
- What happens if you read from a closed channel?