Buffered Channels

Let’s learn about buffered channels.

What are buffered channels?

The topic of this lesson is buffered channels. These channels allow us to put jobs in a queue quickly in order to be able to deal with more requests and process requests later on. Moreover, we can use buffered channels as semaphores in order to limit the throughput of our application.

The presented technique works as follows: all incoming requests are forwarded to a channel, which processes them one by one. When the channel is done processing a request, it sends a message to the original caller saying that it is ready to process a new one. So, the capacity of the buffer of the channel restricts the number of simultaneous requests that it can keep.

Coding example

The file that implements the technique is named bufChannel.go and features the following code:

Get hands-on with 1200+ tech skills courses.