Making sense of channels
Welcome to the channel carnival!
Imagine Go channels as magical, clown-sized pipes that allow circus performers (goroutines) to pass around juggling balls (data) while making sure nobody drops the ball – quite literally!
How to use channels
To use channels, we need to use a built-in function called make()
, informing what type of data we’re interested in passing using this channel:
make(Chan T)
If we want a channel of string
, we should declare the following:
make (chan string)
We can inform a capacity. Channels with capacity are called buffered channels. We won’t bother going into detail about capacity for now. We create an unbuffered channel when we don’t inform the capacity.
An unbuffered channel
An unbuffered channel is a way to communicate between multiple goroutines, and it needs to respect a simple rule – the goroutine that wants to send in the channel and the one that wants to receive should...