Channels
A channel is a communication mechanism that, among other things, allows goroutines to exchange data. Firstly, each channel allows the exchange of a particular data type, which is also called the element type of the channel, and secondly, for a channel to operate properly, you need someone to receive what is sent via the channel. You should declare a new channel using make()
and the chan
keyword (make(chan int)
), and you can close a channel using the close()
function. You can declare the size of a channel by writing something similar to make(chan int, 1)
. This statement creates a buffered channel that has a different use—buffered channels are explained later in this chapter.
Just because we can use channels, it does not mean that we should. If a simpler solution exists that allows goroutines to get executed and save the generated information, we should also consider that. The purpose of every developer should be to create a simple design, not to use all the...