Go language Select Focus Point

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
 PackageMainImport("FMT""Time")funcMain() {c1: = Make(Chanstring) C2: = Make(Chanstring) C3: = Make(Chanstring)varC4Chanstring       C5: = Make(Chanstring)Close(c5) C6: = Make(Chanstring, 3) C6 <-"Buffered 1"       C6 <-"Buffered 2"       C6 <-"Buffered 3"       Close(C6)go func() {time.Sleep(Time.Second*1) C1 <-"One"              Close(c1)//Close C1 Channel} ()go func() {time.Sleep(Time.Second*2) C2 <-"both"       }()go func() {FMT.Println(&LT;-C3)} () for{Select{ CaseMsg1,OK: = <-c1:ifOK {fmt.Println("Received1", MSG1)}Else{C1 =Nil//To determine if the C1 channel has been closed and assigns it to nil, the case will remain blocked, so select continues to evaluate the other case, avoiding the dead loop,                            Fmt.Println("C1 channel has closed.")                     } CaseMSG2: = <-c2://Read FMT from C2 channel.Println("Received2", MSG2) CaseC3 <-"Write String"://write FMT to the C3 channel.Println("C3 Channel wrote.") Case<-C4://Because C4 is nil, this case will remain blocked, so select continues to evaluate other case. Fmt.Println("never reach here.") CaseC4 <-"Never"://Because C4 is nil, this case will remain blocked, so select continues to evaluate other case.Fmt.Println("never reach here.")Case str: = <-c5://Dead loop, continuously read out 0 value, i.e. empty string. FMT. Printf ("Empty:%s", str)Case c5 <-"Panic"://write to the already closed channel, and a panic exception occurs. FMT. Println ("Panic")Case str: = <-c6://Dead loop, after reading the value written in C6, continue to read out the 0 value, the empty string. FMT. PRINTF ("Buffered channel value:%s", str)               CaseC6 <-"Buffered channel closed, write to panic":
< /span>//writes to the already closed channel, a panic exception occurs.  FMT. println  ( "buffered channel closed, write to Panic" ) 
              <-time.  After (Time.  5)://Timer to prevent operation indefinitely blocking                     FMT.  Println("timeout\n")              //default://No default then select is blocking wait, there is default is non-blocking wait, as if Spin-lock.               //     FMT. Println ("default\n")              }}}       
Summary: Each case in select is an IO operation for the channel, read or write, and when multiple case is ready (readable and writable), select randomly chooses an execution.
select{} will be blocked forever, usually select and for use, so that Select can loop continuously evaluate each case, randomly select a case execution.
In fact, the concept of select has been in Linux for a long time, to monitor whether multiple file descriptors are IO operations, when there is a file descriptor IO operation, the Select call returns immediately, the return value includes the occurrence of the IO operation of the file descriptor collection.
Reference: http://tonybai.com/2014/09/29/a-channel-compendium-for-golang/
Note: This article is only my personal notes, if there are errors and omissions, please correct me, study together, my e-mail: htyu_0203_39@sina.com
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.