Working with buffered channels
Earlier in this chapter, we learned that buffered channels would have the cap
property defined with a non-zero integer value. In this section, we will learn how to work with buffered channels and how to share data by establishing communication among coroutines.
Understanding the behavior of a buffered channel
Unlike unbuffered channels, buffered channels are non-blocking channels, and they have a non-zero capacity specified in their declaration. The following code demonstrates a simple example of a buffered channel:
module main fn main() { ch := chan int{cap: 1} defer { ch.close() } ch <- 3 x := <-ch println(x) println('End main') }
In the preceding code, we are creating a buffered channel of the chan int
type with...