Title here
Summary here
Unbuffered channels have no capacity to store values. When you send a value on an unbuffered channel, the operation will block until another goroutine is ready to receive the value.
ch := make(chan int) // Unbuffered channel
// In goroutine 1
ch <- 42 // Blocks until goroutine 2 receives
// In goroutine 2
value := <-ch // Blocks until goroutine 1 sends
This creates a synchronization point between goroutines, ensuring that the sending goroutine and receiving goroutine are synchronized.
Buffered channels have a capacity to store a limited number of values. Sending to a buffered channel only blocks when the buffer is full, and receiving only blocks when the buffer is empty.
ch := make(chan int, 3) // Buffered channel with capacity 3
ch <- 1 // Doesn't block
ch <- 2 // Doesn't block
ch <- 3 // Doesn't block
ch <- 4 // Blocks until someone receives from the channel
Feature | Unbuffered Channels | Buffered Channels |
---|---|---|
Creation | make(chan T) | make(chan T, size) |
Capacity | 0 | Specified size |
Send blocks when | Always (until received) | Buffer is full |
Receive blocks when | Always (until sent) | Buffer is empty |
Synchronization | Strong (rendezvous) | Weaker (queue) |
Use case | Direct handoff, strict synchronization | Decoupling, batching, rate limiting |