官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 三穗县| 定襄县| 仁寿县| 油尖旺区| 朝阳县| 申扎县| 黑山县| 甘泉县| 白银市| 新建县| 白银市| 乡宁县| 怀集县| 象山县| 若羌县| 蓝山县| 和平区| 五台县| 景德镇市| 浑源县| 安丘市| 格尔木市| 武穴市| 怀安县| 锡林郭勒盟| 诸城市| 恩施市| 红原县| 新源县| 台中县| 山阴县| 沧源| 东海县| 焦作市| 沙洋县| 古丈县| 休宁县| 松江区| 浙江省| 永济市| 邢台市|