Golang Channel Comments

Thank refer to the original -http: //bjbsair.com/2020-03-27/tech-info/7058/
Introduction

Goroutine and Channel are the two cornerstones of concurrent programming language Go. Goroutine for executing concurrent tasks, Channel for synchronizing communication between goroutine.

In Golang concurrent philosophy, there is a very famous words:

Do not communicate by sharing memory; instead, share memory by communicating.

Means: Do not communicate through shared memory, and shared memory to be achieved through communication, it relies CSP (Communication Sequence Process) model, referred to as communicating sequential processes.

Go advocate the use of methods of communication instead of shared memory, when a Goroutine Goroutine needs and other resource sharing, Channel will bridge the gap between them and provide security mechanisms to ensure synchronization.

Golang Channel detailed analysis

Channel on a queue or essentially follow FIFO (First In-First Out) principle,

Create a channel

Create a channel keywords are needed for the make , format is as follows:

通道实例 := make(chan 数据类型)
  • Data type: type of element within the transmission channel.
  • Channel Example: channel handle created by make.

Use the channel

After the channel is created, you can use the channel for transmit and receive operations.

Write

Write channel use a special operator <-, format data is transmitted through the channel:

通道变量 <- 值

(May be simply understood as the direction of the arrow to transfer the value of the final destination)

// 创建一个空接口通道  
ch := make(chan interface{})  
// 将0放入通道中  
ch <- 0  
// 将hello字符串放入通道中  
ch <- "hello"

Read

Use the same read channel <- operator, receiving channel has the following characteristics:

  1. Transceiving operation is performed between different channels of two goroutine.
  2. Since the data channel in the absence of the receiving side processing, data transmission side will continue to clog, so the channel must be received in another of goroutine.
  3. It will continue receiving the transmission data block until the transmission side.

Receiving a data channel There are 4 wording:

  1. Blocking reception

Blocking data reception mode, a received value only, in the following format:

data := <-ch

When the statement is executed program will block until data is received and assigned to the variable data.

  1. Non-blocking receive data

Use nonblocking when receiving data from the channel, the statement does not occur clogging, the following format:

data, ok := <-ch

data: indicates that the received data. When data is not received, data of zero value channel type.

ok: it indicates whether the received data.

Features: non-blocking channel reception method may cause high CPU usage, so is not recommended for use.

  1. Ignore the data received

Ignore any data returned from the channel, in the following format:

<-ch

Features: This method is blocked, the program must wait until the return channel will continue to go down.

  1. Receiving loop

Receiving data channel can be borrowed for range statement plurality of elements receiving operation, the following format:

for data := range ch {  
    // do sth.  
}

Ch is the channel can be traversed, traversing the result is the received data. Data type is the data type of channel. Traversing variables obtained for only one, i.e. in the above example data.

Read / Write Only channel

In general, the channel is bidirectional, i.e., data can be entered and output. However, in order to meet certain specific business scenarios, the official also provides support only reading (Read Only) or only supports write (Write Only) channel format is as follows:

//定义只读通道  
ch_r := <-chan interface{}  
//定义只写通道  
ch_w := <-chan interface{}

Channel buffer

Go language channel buffer (buffered channel) is a channel that can store one or more values ​​before being received. This type of passage is not mandatory to be completed between the sending and receiving simultaneously goroutine. Channel conditions will block transmission and reception operation will be different. Only there is no value in the channel to be received, the reception operation will be blocked. Only when the value of the buffer receiving channel is not available to be transmitted, the transmission operation will be blocked.

Channel buffer is defined as follows:

通道实例 := make(chan 通道类型, 缓冲大小)
  • Channel Type: unbuffered and channel usage is consistent, affecting the data transmission and reception of the channel type.
  • Buffer Size: determines the number of channels can save up elements of.
  • Channel instance: being created out of the channel instance.

Here I borrow the following picture to illustrate this principle channel

Golang Channel detailed analysis

  1. The left goroutine continue to plug data channel
  2. goroutine continue to take the right data from the channel
  3. A read / write, sync
  4. The left goroutine all the data has been put over, but this time the remaining data channel also, goroutine all the right still working

Unbuffered channel

Go language unbuffered channel (unbuffered channel) that are not the ability to save the value of the channel before receiving any. This type of transmission channel requirements goroutine goroutine simultaneously receiving and ready to complete the transmit and receive operations.

Unbuffered mode channel is defined as follows:

通道实例 := make(chan 通道类型)
  • Channel Type: unbuffered and channel usage is consistent, affecting the data transmission and reception of the channel type.
  • Buffer Size: 0
  • Channel instance: being created out of the channel instance.

To put it more clearly, I also found a piece of additional chart to illustrate:

Golang Channel detailed analysis

In step 1, two arrival channels are goroutine, but which did not begin transmission or reception. In step 2, goroutine it to the left hand into the channel, which simulate the behavior of the channel to transmit data. In this case, the channel will be locked goroutine until exchange is complete. In step 3, goroutine the right hand it into the channel, which is received from the channel in the analog data. Like this goroutine will be locked in the channel until the exchange is complete. In step 4 and 5, exchange, and ultimately in step 6, are two goroutine their hands out from the channel in which the analog is released goroutine locked. Two goroutine can now do other things.

to sum up

GoLang channel is an important tool to support concurrent stable and efficient operation of the system, only fully understand it, to find the most critical programs in business development and troubleshooting. Know thyself, know yourself.

Original articles published 0 · won praise 0 · Views 267

Guess you like

Origin blog.csdn.net/zxjoke/article/details/105140013