Buffers
You’ve seen in the previous exercises that there are channels with a defined length and channels with an undetermined length:
ch1 := make(chan int) ch2 := make(chan int, 10)
Let’s see how we can make use of this.
A buffer is like a container that needs to be filled with some content, so you prepare it when you expect to receive that content. We said that operations on channels are blocking operations, which means the execution of the Goroutine will stop and wait whenever you try to read a message from the channel. Let’s try to understand what this means in practice with an example. Let’s say we have the following code in a Goroutine:
i := <- ch
We know that before we can carry on with the execution of the code, we need to receive a message. However, there is something more about this blocking behavior. If the channel does not have a buffer, the Goroutine is blocked as well. It is not possible to write to a channel or to receive a...