Synchronous and Asynchronous Communication
Explore how Go manages concurrency through synchronous blocking via unbuffered channels and asynchronous communication using buffered channels. Learn the mechanics of goroutine synchronization, deadlocks, and how buffering improves program scalability and responsiveness. This lesson equips you with practical knowledge to design efficient concurrent applications in Go.
We'll cover the following...
Blocking of goroutines
By default, communication is synchronous, and unbuffered, which means the send operation does not complete until there is a receiver to accept the value. One can think of an unbuffered channel as if there is no space in the channel for data. There must be a receiver ready to receive data from the channel, and then the sender can hand it over directly to the receiver. So, channel send/receive operations block until the other side is ready:
- A send operation on a channel (and the goroutine or function that contains it) blocks until a receiver is available for the same channel
ch. If there’s no recipient for the value onch, no other value can be put in the channel, which means no new value can be sent inchwhen the channel is not empty. So, the send operation will wait untilchbecomes available again. This is the case when the