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

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.

主站蜘蛛池模板: 东明县| 南安市| 建瓯市| 樟树市| 新绛县| 册亨县| 安图县| 新野县| 湟中县| 乐至县| 海林市| 浦北县| 南皮县| 丹巴县| 惠水县| 通辽市| 即墨市| 隆德县| 林口县| 南昌县| 那曲县| 翁牛特旗| 汤阴县| 迭部县| 佛坪县| 拉萨市| 类乌齐县| 清水县| 开江县| 永州市| 洛浦县| 从化市| 浮山县| 色达县| 阿拉善左旗| 女性| 塘沽区| 普安县| 大洼县| 靖州| 汝城县|