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

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.

主站蜘蛛池模板: 长乐市| 榆社县| 宁津县| 武乡县| 宜城市| 旬阳县| 洛扎县| 湘潭县| 汾西县| 互助| 玛纳斯县| 巴东县| 尚志市| 怀集县| 广丰县| 乌拉特后旗| 阿克陶县| 四平市| 瓦房店市| 华宁县| 南乐县| 和田县| 岳西县| 来宾市| 铜山县| 甘肃省| 察哈| 湟源县| 邵武市| 逊克县| 云南省| 北宁市| 会同县| 黔西| 淮南市| 确山县| 天等县| 兴文县| 贞丰县| 赤水市| 施秉县|