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

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.

主站蜘蛛池模板: 夹江县| 东山县| 静海县| 晋宁县| 若羌县| 宜兴市| 乐山市| 绥滨县| 屏东市| 习水县| 绵阳市| 庆元县| 河南省| 罗田县| 荔浦县| 盐亭县| 瓦房店市| 安新县| 藁城市| 九龙县| 永新县| 贵定县| 富平县| 武定县| 铜陵市| 罗源县| 阳城县| 阿鲁科尔沁旗| 邻水| 吕梁市| 长海县| 独山县| 新津县| 南部县| 凌海市| 仲巴县| 普安县| 太谷县| 乐山市| 寻乌县| 彩票|