Usage of golang select the

1 go channel

1.1 What is the channel

Channel may be understood as the communication channel between the go coroutine.

1.2 Statement channel

All channels are associated with a type, once associated with the type, the channel can only transmit the data type, other types of data transmission word it is illegal.

chan T t denotes the type of channel.

var b chan int, declare a channel b

1.3 defines a channel

b := make(chan int)

1.4 transmitting and receiving data over the channel

Arrow indicates the channel away from the receive data channel: data: = <- b

Arrows point to the channel, then, it is to send data: b <- data

1.5 transmitting and receiving channel is blocked default

When the data is transmitted to the channel, the program control statement to be blocked in the transmitting data, until another data read go coroutine will unblock from the channel. When the read channel, if no other data is written to the coroutine channel, the read process will be blocked.

2 What is the select

select switch statement for communication, each case is a channel operation, if data is read and then select a random selection performed, and then select the entire exit, if not read, then blocked, or perform default, then exit .

In other words, execution is performed only once select a branch or default in the case.

 

Guess you like

Origin www.cnblogs.com/hustdc/p/11315759.html