Small talk about channel
If we talk about concurrency in Golang, Golang provides us with a type of concurrency communication called
Channel. Channel itself helps us to communicate between goroutines. We can send and receive messages from one goroutine to another. There are two types of channel in golang that we can used and let’s talk about them.
Unbuffered channel is a channel that
initially has no capacity to store message inside it.
Unbuffered channel requires us to fill the message in order to make the goroutine process unblocked by the channel. For example:
In the example above, our main function blocked when filling
ch one by one. And our
access function will print the message filled by
main function one by one also because of the for loop.
Here is the output of the example above:
Unlike the Unbuffered Channel,
Buffered Channel has a capacity to store messages inside it.
Buffered Channel could be filled up to its defined capacity, not only one message. For example:
Here is the output:
As you can see, the
ch could be filled first until it is full-capacity then the other goroutine could access it one by one.
Unbuffered Channel has no capacity initially, but
Buffered Channel has a capacity.
Unbuffered Channel will block the goroutine whenever
it is empty and waiting to be filled. But
Buffered Channel will also block the goroutine either when
it is empty and waiting to be filled or
it's on its full-capacity and there's a statement that want to fill the channel.
Empty and Waiting to be filled
Both Buffered and Unbuffered channel blocks the goroutine until it is filled in this case blocks for a second.
Full Capacity and there’s statement that want to fill the channel
Buffered channel blocks on
ch <- 2 because the
ch is on its full capacity and waiting to be released. The
Unbuffered Channel will go deadlock this time because there’s no goroutine accessing the message
2 but it is filled.
Thank you for reading!