Golang Simple understanding of Channel

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

What is Channel?

The channel is the way that the go language communicates between the goroutine provided at the language level. We can use the channel to pass messages between two or more goroutine. Channel is the process of communication, so the process of passing objects through the channel and call the function of the parameter transfer behavior is more consistent, such as can also pass pointers and so on. If you need to communicate across processes, we recommend a distributed system approach, such as using a socket or HTTP communication protocol. The go language also has a very good support for the network aspect. Channel is type-dependent. In other words, a channel can only pass a value of one type, which needs to be specified when declaring the channel. If you know something about UNIX pipelines, it is not difficult to understand the channel, which can be considered a type-safe conduit.

(I copy it on the PDF.) personal Understanding is a communication mechanism of each thread in concurrent programming.


Code:

package mainimport  "FMT" Func sum (values []int, resultchan chan int)  {     sum := 0    for _, value := range  values {        sum += value     }    //  send calculation results to channel     resultchan <- sum} Func main ()  {    values := []int{1, 2, 3, 4, 5,  6, 7, 8, 9, 10}    resultchan := make (Chan int,  3)     go sum (Values[:len (values)/2], resultchan)     go  sum (Values[len (values)/2:], resultchan)     go sum (Values[len (values)/3:],  resultchan)     sum1, sum2, sum3 := <-resultchan, <- Resultchan, <-resUltchan    fmt. Println ("Result:", &NBSP;SUM1,&NBSP;SUM2,&NBSP;SUM3)}


The sum function .

2 parameters, one is an int array, and one is an int type chan~~~ function, even if the passed array is added individually, it is not said.

The back of the Resultchan <-sum here is to assign the result of our addition to the Resultchan inside


Main function

Values: = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Here is the definition of an int array

Resultchan: = Make (chan int, 3)

declares an int-type Chan with a size of 3

Go sum (Values[:len (values)/2], Resultchan)

Take half of the values array, Resultchan. Then start a thread to access the SUM function

Go sum (Values[len (values)/2:], Resultchan)

Take half of the values array, Resultchan. Then start a thread to access the SUM function

Go sum (Values[len (values)/3:], Resultchan)

Computes the length of the array by 10, and then the other 3. Infinite loop 3.333333~, the system automatically takes 3, followed by a colon action to take the data after the 3rd element

Sum1, sum2, sum3: = <-resultchan, <-resultchan, <-resultchan

Assign the value of Resultchan to SUM1,SUM2,SUM3. According to the order of thread execution

Fmt. Println ("Result:", Sum1, sum2, sum3)

Output results


How do you use Chan?

Write a simple example, please.

package mainimport  "FMT" Func sum (arrays []int, ch chan int)  {     //fmt. PRINTLN (arrays)     sum := 0    for _, array : = range arrays {        sum += array     }    ch <- sum}func main ()  {     arraychan := make (chan int, 20)     arrayint := []int{1 ,  2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,  15}    for t := 0; t < 10; t++ {         length := len (Arrayint)          go sum (Arrayint[length-t:], arraychan)     }   & NBsp;arrayresult := [10]int{0}    for i := 0; i <  10; i++ {        arrayresult[i] = <-arraychan     }    fmt. Println (Arrayresult)}

The general idea is to declare a Chan, then start 10 threads, put the results in Chan, and finally put the data in Chan into an array and loop the output.

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.