This is a creation in Article, where the information may have evolved or changed.
The previous article talked about the process and the pipeline
This article is mainly about SELECT, a useful keyword to listen to the pipeline, incidentally, with a buffered channel.
Channel with Buffer
The channel is a buffered c: = Make (chan int,3) So the statement represents the maximum number of simultaneous three int types of data in this pipeline.
As shown in the following code
Package MainFunc Main () {c: = Make (chan int,3) C <-1c <-1c <-1
<span style= "White-space:pre" ></span>//c <-1 If you add this sentence, you will get an error because you cannot put four int type data <-c<-c<-C}
Select
Go provides the SELECT keyword for listening to each channel
For example, I'm going to take the data from Channel 1, and I'm taking it out of the data, so how do we make it work?
This is a feature that can be used to listen to a select to automatically get the data out if it comes in.
Package Mainimport ("Time", "FMT") func main () {o: = make (chan int) c: = Do (chan int) go func () {for {select {case A: = <- c://Monitor C pipeline just print out the FMT as soon as the data comes in. Println (a)//here After return <-chan time is the monitor <-chan time this pipe//if more than 5 seconds if Select has not received the message then it will send a message to the <-chan time Channel// Case <-time is sent every 5 seconds. After (5 * time. Second): o <-0break//Just jump out of the Select loop and do not jump out for Loop}}} () for I: = 0; i<100; I++{c <-i}<-o//Receive Message}