A buffered channel in Go is a channel that can store one or more values before being received. This type of channel does not mandate that both the goroutine must be sent and received at the same time. The conditions for the channel to block the sending and receiving actions will be different. The receiving action will block only if there is no value to be received in the channel. The sending action will only block if the channel has no available buffer to accommodate the sent value.
This results in a big difference between buffered channels and unbuffered channels: unbuffered channels ensure that the goroutines that are sent and received will exchange data at the same time; buffered channels do not have this guarantee.
On the basis of unbuffered channels, add a finite size storage space to the channel to form a buffered channel. The buffered channel completes the transmission process without waiting for the receiver to receive when sending, and does not block, and blocks only when the storage space is full. Similarly, if there is data in the buffer channel, there will be no blockage during reception until there is no data in the channel to be read, the channel will block again.
The unbuffered channel ensures synchronization of the transmission and reception process. The unbuffered delivery process is similar to the courier calling you to go downstairs to pick up the express. The entire process of submitting the express delivery occurs simultaneously, and you and the courier will see you all the time. But in doing so, the courier must wait for everyone to go downstairs to complete all delivery work. If the courier puts the express delivery into the express cabinet and notifies the user to pick it up, the courier and the user will become an asynchronous delivery process, and the efficiency can be significantly improved. The buffered channel is such a "express cabinet".
Create a buffered channel
How to create a buffered channel? See the following code:
Channel instance:= make(chan channel type, buffer size)
- Channel type: Consistent with the usage of unbuffered channels, affecting the data type sent and received by the channel.
- Buffer size: Determines the maximum number of elements that the channel can save.
- Channel instance: The created channel instance.
The following is an example to understand the usage of buffered channels, see the following code:
package main import "fmt" func main() { // Create an integer channel with 3 element buffer sizes ch := make(chan int, 3) // Check the size of the current channel (len(ch)) // Send 3 integer elements to the channel ch <- 1 ch <- 2 ch <- 3 // Check the size of the current channel (len(ch)) }
The code output is as follows:
0
3
The code description is as follows:
- Line 8, create a channel of integer type with 3 element buffer sizes.
- Line 11, view the size of the current channel. When the buffered channel is created, the internal elements are empty, so the return value obtained using len() is 0.
- Lines 14-16, send 3 integer elements to the channel. Because buffered channels are used. Even without goroutine reception, the sender will not block.
- Line 19, since 3 channels are filled, the channel length at this time becomes 3.
Blocking conditions
Buffered channels are similar to unbuffered channels in many characteristics. Unbuffered channels can be regarded as buffered channels with a length of always 0. Therefore, according to this feature, the buffered channel will still block under the circumstances listed below:
- When the buffered channel is filled, blocking occurs when trying to send data again.
- When the buffered channel is empty, blockage occurs when trying to receive data.
Why does Go language limit the length of the channel without providing an infinite length channel?
We know that the channel is a bridge of communication between two goroutines. The code that uses goroutine must have one party providing the data and the other party consuming the data. When the data supply speed of the data side provided by the data is greater than the data processing speed of the consumer, if the channel does not limit the length, the memory will continue to swell until the application crashes. Therefore, limiting the length of the channel is conducive to constraining the supply speed of the data provider. The amount of supplied data must be within the range of the consumer processing volume + the channel length in order to process the data normally.
This is the end of this article about the use of the Go language with buffered channels. For more related content with the Go language with buffered channels, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!