- 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.
推薦閱讀
- 零基礎(chǔ)學C++程序設(shè)計
- 摩登創(chuàng)客:與智能手機和平板電腦共舞
- Effective C#:改善C#代碼的50個有效方法(原書第3版)
- Python從入門到精通(精粹版)
- Android 7編程入門經(jīng)典:使用Android Studio 2(第4版)
- Monitoring Elasticsearch
- Learning Selenium Testing Tools(Third Edition)
- 零基礎(chǔ)學Python網(wǎng)絡(luò)爬蟲案例實戰(zhàn)全流程詳解(入門與提高篇)
- Learning Laravel's Eloquent
- 從零開始學Linux編程
- Java程序員面試筆試寶典(第2版)
- JavaScript應(yīng)用開發(fā)實踐指南
- Flowable流程引擎實戰(zhàn)
- Buildbox 2.x Game Development
- 移動增值應(yīng)用開發(fā)技術(shù)導論