What are channels in Go and how do they enable communication between goroutines?
Learn what Go channels are, how buffered and unbuffered channels sync goroutines, and how select enables safe concurrent communication without locks.
Expected Interview Answer
A channel is a typed conduit that lets goroutines send and receive values safely, providing both communication and synchronization so you share data by communicating rather than by sharing memory.
Channels are created with make and can be unbuffered or buffered. On an unbuffered channel a send blocks until another goroutine receives, which synchronizes the two; a buffered channel allows a fixed number of values to queue before sends block. Combined with the select statement, channels let goroutines coordinate, fan work out, collect results, and signal completion without explicit locks, embodying Go's motto: don't communicate by sharing memory, share memory by communicating.
- Type-safe communication between goroutines
- Built-in synchronization without manual locks
- Unbuffered channels coordinate send/receive timing
- Buffered channels decouple producers and consumers
- Work naturally with select for multiplexing
- Closing a channel broadcasts completion to receivers
AI Mentor Explanation
A channel is like the single ball passed between fielders during a relay throw to the stumps: only one player holds it at a time, and the next cannot act until it is received. This handoff both moves the ball and synchronizes the players, exactly as a channel moves a value and coordinates two goroutines.
Step-by-Step Explanation
Step 1
Create a channel
Use make(chan T) for an unbuffered channel or make(chan T, n) for a buffered one carrying values of type T.
Step 2
Send and receive
ch <- v sends a value; v := <-ch receives one. On unbuffered channels these operations rendezvous and block until paired.
Step 3
Buffering
A buffered channel lets up to n values queue, so sends only block when the buffer is full and receives only block when it is empty.
Step 4
Select
Use select to wait on multiple channel operations at once, handling whichever is ready and optionally a default case.
Step 5
Closing
close(ch) signals no more values; receivers get the zero value and ok=false, and range over a channel ends when it closes.
What Interviewer Expects
- Defining channels as typed conduits for goroutine communication
- Explaining the difference between unbuffered and buffered channels
- Understanding that channels also synchronize goroutines
- Knowing the select statement's role
- Awareness of closing channels and the comma-ok receive
Common Mistakes
- Sending on a closed channel (causes a panic)
- Forgetting an unbuffered send blocks until a receiver is ready
- Not closing channels, leading to goroutine leaks or deadlocks
- Confusing buffered capacity with unlimited storage
- Using channels where a simple mutex would be clearer
Best Answer (HR Friendly)
“Channels are Go's built-in way for concurrent tasks to safely pass data to each other, like a pipe that one task sends into and another reads from. Because the send and receive coordinate automatically, developers avoid many of the bugs that come from tasks fighting over shared memory.”
Code Example
package main
import "fmt"
func square(nums []int, out chan<- int) {
for _, n := range nums {
out <- n * n
}
close(out)
}
func main() {
out := make(chan int)
go square([]int{1, 2, 3}, out)
for v := range out {
fmt.Println(v)
}
}Follow-up Questions
- What is the difference between a buffered and unbuffered channel?
- What happens when you send to a closed channel?
- How does the select statement work?
- How can channels cause a deadlock?
- When would you use a channel versus a sync.Mutex?
MCQ Practice
1. What happens on a send to an unbuffered channel with no ready receiver?
An unbuffered send blocks until another goroutine receives, which synchronizes the two goroutines.
2. What does sending on a closed channel do?
Sending on a closed channel causes a runtime panic; only sends, not receives, panic after close.
3. Which statement lets a goroutine wait on multiple channels at once?
The select statement blocks until one of its channel cases is ready, then runs that case.
Flash Cards
What is a Go channel? — A typed conduit for sending and receiving values safely between goroutines.
Unbuffered vs buffered channel? — Unbuffered blocks until send and receive pair up; buffered queues up to n values before blocking.
What does closing a channel do? — Signals no more values; receivers get the zero value with ok=false and range loops end.
Go's concurrency motto? — Don't communicate by sharing memory; share memory by communicating.