- 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.
推薦閱讀
- Big Data Analytics
- Flux Architecture
- HTML5入門經(jīng)典
- D3.js By Example
- Internet of Things with ESP8266
- Hands-On Nuxt.js Web Development
- 深入實(shí)踐Kotlin元編程
- 深度學(xué)習(xí)原理與PyTorch實(shí)戰(zhàn)(第2版)
- Clojure High Performance Programming(Second Edition)
- C語言從入門到精通
- AutoCAD基礎(chǔ)教程
- 實(shí)驗(yàn)編程:PsychoPy從入門到精通
- 例說FPGA:可直接用于工程項(xiàng)目的第一手經(jīng)驗(yàn)
- Getting Started with Web Components
- 從零開始學(xué)算法:基于Python