- Hands-On Full Stack Development with Go
- Mina Andrawos
- 167字
- 2021-07-02 12:33:34
Buffered channels
A buffered channel is a special type of channel that holds a buffer that contains a number of items. Unlike a regular channel, a buffered channel doesn't block unless the following takes place:
- Its buffer is empty and we are trying to receive a value from the channel.
- Its buffer is full and we are trying to send a value to the channel.
To declare a buffered channel, we use the following syntax:
myBufferedChannel := make(chan int,10)
The preceding syntax creates a buffered channel that can hold 10 int values.
To send a value to the buffered channel, we use the same syntax as with regular channels. Each send operation adds one item to the buffer, as follows:
myBufferedChannel <- 10
To receive a value from a buffered channel, we utilize the same syntax as well. Each receive operation removes one item from the buffer, as follows:
x := <-myBufferedChannel
Let's take a look at the select statement construct in the next section.
推薦閱讀
- Dynamics 365 for Finance and Operations Development Cookbook(Fourth Edition)
- JBoss Weld CDI for Java Platform
- 劍指Offer(專項(xiàng)突破版):數(shù)據(jù)結(jié)構(gòu)與算法名企面試題精講
- Java高手真經(jīng)(高級(jí)編程卷):Java Web高級(jí)開發(fā)技術(shù)
- Python高級(jí)編程
- Mastering Ext JS
- Android應(yīng)用案例開發(fā)大全(第二版)
- Mastering Business Intelligence with MicroStrategy
- Vue.js 3應(yīng)用開發(fā)與核心源碼解析
- Flink技術(shù)內(nèi)幕:架構(gòu)設(shè)計(jì)與實(shí)現(xiàn)原理
- Machine Learning for OpenCV
- .NET 4.0面向?qū)ο缶幊搪劊簯?yīng)用篇
- Python 快速入門(第3版)
- Monitoring Docker
- 游戲設(shè)計(jì)的底層邏輯