O'Reilly logo

Distributed Computing with Go by V.N. Nikhil Anurag

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The buffered channel

Consider the case where we are able to send more messages into a channel than the goroutines receiving the messages can handle them. If we use unbuffered channels, it would significantly slow down the program because we will have to wait for each message to be processed before we can put in another message. It would be ideal if the channel could store these extra messages or "buffer" the messages. This is exactly what a buffered channel does. It maintains a queue of messages which a goroutine will consume at its own pace. However, even a buffered channel has a limited capacity; we need to define the capacity of the queue at the time of channel creation.

So, how do we use a buffered channel? Syntax-wise, it is identical ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required