Go learning notes _routine and Channel

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Go language Concurrent programming

Above from the basic level of the goroutine and channel , on how to use all stay under the lip, so the next section to choose a few of the simplest examples to consolidate. In addition to the recommendation of their own personal blog, you can first point to see if the content is helpful to you, in the selection of collections or Direct X out.

Personal blog Address: colourful Code

Body

First, there are two types of channel :

    • Cache Not Allocated

    • Allocate a certain cache

The example is given at the beginning of these two points.

Cache Not Allocated

 PackageMainImport "FMT"//Sum function sumfuncSum (A []intCChan int) {sum:=0     for_,v: =Rangea{sum + = v} c<-sum//Write data value sum to channel C}//Main program entryfuncMain () {a:= []int{1, 3, 5, 2, -2} c:= Make(Chan int)GoSUM (a[:],c)//Open a goroutine for the SUM function executionx:= <-c//Read C-Channel value assignment to xFmt. PRINTLN (x)}

Starting the analysis from the main program, the following steps are divided into:

  • The declaration defines an array a

  • The declaration defines a channel C, noting that there is no given cache.

  • Because the go keyword is added, a goroutineis opened for the sum function, and arguments are passed to the function: array a and channel C. At this time the program is in addition to the main routine, which is the main function. At the same time there are sub- routinefor sum , and the routine is to sum the incoming array and write the result value to channel C .

  • After the main routine executes go sum(a[:],c) this statement, the next statement can be executed immediately. That means x:=<-c . It's a little bit interesting here. According to the actual situation in 2 kinds of cases:C Channel has value and no value.

    1. The C channel has a value, which means that the sum operation in the sub- goroutine is completed first, when the result value is written to the channel through the statement, c<-sum because the completion is too early, no receiver from the channel to read the data caused by the blockage, sub- routine the card in the program dies here, waiting for the receiver to read the data. Until the main routine executed x:=<-c This statement, successfully read from the channel to the data, at the same time sub -routine c<-sum because the channel data is read away, here no longer blocked, sub- routine the sum function in is finished executing.
    2. The C -channel has no value, which means that the sum operation in the sub- Goroutine is not completed (if the sum operation here is changed to big data processing, I want to spend a lot of time.) ), the data has not been written to the C channel, so because there is no data in the channel, the main routine will be blocked on x:=<-c this statement, waiting for the sub- routine to complete the data calculation, execution c<-sum Statement to write data to the channel. At this time, the main routine successfully read the value in Channel C , assigned to X, printed out.

Allocation cache

package mainimport"fmt"func main(){    c:=make(chanint,2)    c <- 10    c <- 20    fmt.Println(<-c)    fmt.Println(<-c)}

The allocation cache is easier to understand because of the understanding of channel working mechanisms.

    • First, declare a channel C that defines a specific type of int with a cache size of 2

    • To write a value of 10 to the channel, think that if the cache is not allocated, then the program will be blocked here, unless the other routine will read the data from that channel, but this is obviously not possible, because this is in the main routine , you do not create a child Routine, so the end will only error,"All Goroutines is asleep-deadlock!"

    • At this time you should have understood, because the allocation of the cache, so can write a value of 10, next to the channel write value 20, obviously because the cache has space, write no problem, will not cause congestion, the program continues to execute!

    • <-cis to read the data from the channel and print 10

    • Print 20, there is a FIFO(Note: "First in the first out", into-and-out) feeling, because we write to the channel cache value 10, and then write the value 20, when the check out should have a sequential first take out the value of 10, and then remove the value 20.

End:

Bo Master is a just into the pit of Go small rookie, to go language understanding may not be very thorough, if where understanding is wrong, or write a problem, welcome correct, step on more health, but I hope you can message tell me why step on the reason, so that I correct, this is also a kind of learning is not it? Last Thank you!

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.