Unbuffered channels
An unbuffered channel has capacity 0: make(chan T). A send completes only when another goroutine is ready to receive. That rendezvous is both data transfer and a synchronization point.
Detailed Explanation
ch := make(chan int) (or make(chan int, 0)) does not store a value. The sender waits in the scheduler until a receiver runs <-ch, then the value is copied over and both continue. If you need "hand this off and do not continue until someone took it," you want unbuffered.
It is a lock-step queue of length zero. There is no "send and forget." If the receiver is busy, the sender parks. If the sender is late, the receiver parks. Deadlock in main with no other goroutines is the classic mistake: ch := make(chan int); ch <- 1 never finds a receiver.
Signalling without data. chan struct{} is the usual "token." done <- struct{}{} or close(done) both work; close is broadcast to every receiver, a send wakes one. Prefer close for "this work is finished" when many waiters exist.
Backpressure. Unbuffered channels apply backpressure automatically: a slow consumer slows the producer. That is a feature for pipelines. If you need a burst of sends, use a buffer, and still decide what happens when the buffer fills.
What it costs. Each handoff is a schedule point. That is more expensive than incrementing an atomic, and cheaper than an unbounded queue that hides a stalled consumer. Use unbuffered when the handshake is the protocol; use buffered when you have a measured burst size.
Code Examples
package main
import "fmt"
func main() {
ch := make(chan string)
go func() {
fmt.Println("sending")
ch <- "ok"
}()
fmt.Println(<-ch)
}func handshake() {
done := make(chan struct{})
go func() {
// work
close(done)
}()
<-done
}func deadlock() {
ch := make(chan int)
ch <- 1 // no receiver, main blocks forever
}Common Gotchas
Send with no concurrent receiver deadlocks that goroutine
in `main`, it hangs the process.
Unbuffered is not "faster"
it is stricter. A buffer of 1 is a different protocol.
Closing still works
receivers get zeros after close, same as buffered.
`make(chan T)` and `make(chan T, 0)` are the same capacity
`make(chan T)` and `make(chan T, 0)` are the same capacity