This is a creation in Article, where the information may have evolved or changed.
One of the most important communication channels in Go is channel
1. Send data to a nil channel, causing permanent blocking
2. Receive data from a nil channel, causing permanent blocking
3. Send data to an already closed channel, causing panic
4. Receive data from an already closed channel and immediately return a value of 0
PackageMainImport "FMT"//This channel does not have the cache set and will be blocked, so it is all executed by defaultfuncMain () {messages: = Make(Chan string) Signals: = Make(Chan BOOL)Select{ CaseMsg: = <-messages:fmt. Println ("received message", msg)default: FMT. Println ("no message Received")} msg: ="HI" Select{ CaseMessages <-msg:fmt. Println ("sent Message", msg)default: FMT. Println ("no message Sent") }Select{ CaseMsg: = <-messages:fmt. Println ("received message", msg) CaseSIG: = <-signals:fmt. Println ("Received signal", SIG)default: FMT. Println ("No Activity") }}//Output//No message received//No message sent//No Activity
Closing Channels.
PackageMainImport "FMT"//This example uses the jobs pipeline to monitor when performing Workder's work//When jobs is closed, the worker finishes executing and starts exiting Goroutine//Goroutine exit and use done to listenfuncMain () {jobs: = Make(Chan int,5) Done: = Make(Chan BOOL)Go func() { for{J, more: = <-jobsifmore {FMT. Println ("Received Job", j)}Else{FMT. Println ("received all Jobs") Done <-true return} } }()//Send three jobs worker to execute forJ: =1; J <=3; J + + {jobs <-J Fmt. Println ("Sent Job", j)}Close(jobs) FMT. Println ("Sent all jobs")//worker is closed<-done}//Output//Sent Job 1//Received Job 1//Sent Job 2//Received Job 2//Sent Job 3//Received Job 3//Sent all jobs//Received All jobs
Range over Channels.
package mainimport Span class= "hljs-string" > "FMT" //there are two ways to traverse the channel: Select and Range //select in the channel input or output to go case //range can be traversed and even close after the value func Main () {queue: = make (chan string , 2 ) queue <- "one" queue <- "one" close (queue) for elem: = range Queue {FMT. Println (elem)}}//output //one //
Timers
PackageMainImport "Time"Import "FMT"//time. Newtimer timed once//Description time is blocking outputfuncMain () {//timing 2sTimer1: = time. Newtimer (time. Second *2)//Block output<-timer1. C FMT. Println ("Timer 1 Expired") Timer2: = time. Newtimer (time. Second)Go func() {<-timer2. C FMT. Println ("Timer 2 Expired")} () Stop2: = Timer2. Stop ()ifSTOP2 {fmt. Println ("Timer 2 Stopped") }}//Output//Timer 1 expired//Timer 2 stopped
Tickers
PackageMainImport "Time"Import "FMT"//Ticker timing cyclefuncMain () {ticker: = time. Newticker (Time.millisecond * -)Go func() { forT: =RangeTicker. C {fmt. Println ("Tick at", T)}} () time. Sleep (Time.millisecond * the) Ticker. Stop () fmt. Println ("Ticker stopped")}//Output//Tick at 2012-09-23 11:29:56.487625-0700 PDT//Tick at 2012-09-23 11:29:56.988063-0700 PDT//Tick at 2012-09-23 11:29:57.488076-0700 PDT//Ticker stopped
Original: Https://gobyexample.com/non-blocking-channel-operations