
go channels are synchronous by default—sending to an unbuffered channel blocks until another goroutine receives from it, causing a deadlock if no receiver is ready.
go channels are synchronous by default—sending to an unbuffered channel blocks until another goroutine receives from it, causing a deadlock if no receiver is ready.
In Go, channels are designed for safe communication between goroutines—and their behavior depends critically on whether they are buffered or unbuffered.
By default, make(chan string) creates an unbuffered channel, meaning it has zero capacity. With such a channel, a send operation (messages block indefinitely until another goroutine is ready to receive (
fatal error: all goroutines are asleep - deadlock!
This is not a bug—it’s Go’s intentional synchronization guarantee: unbuffered channels enforce handshake-style communication (also called synchronous rendezvous), ensuring both sender and receiver are ready at the same time.
✅ Fix: Use a Buffered Channel
To allow the send to proceed without an immediate receiver, allocate buffer space:
func main() {
messages := make(chan string, 1) // buffered with capacity 1
messages <p>Here, the channel holds up to one string. The send succeeds immediately, and the subsequent receive retrieves it safely.</p><h3>⚠️ Important Notes</h3>
- Buffer size must be a non-negative integer; make(chan T, 0) is equivalent to make(chan T) (unbuffered).
- Increasing buffer size (e.g., make(chan string, 10)) allows up to that many sends before blocking—but does not eliminate the need for eventual receives. Unread values accumulate and may cause memory pressure or logical bugs.
- For true concurrency (e.g., sending and receiving in parallel), use goroutines:
func main() {
messages := make(chan string) // unbuffered still works here!
go func() {
messages <p>In summary: <strong>unbuffered = sync handshake; buffered = async queue (up to capacity)</strong>. Choose based on whether you need coordination (e.g., signaling) or decoupled data flow (e.g., producer-consumer pipelines).</p>











