go-bufferchannel-select-Basic Understanding

Source: Internet
Author: User
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}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.