Go Concurrent Programming Basics (i) Goroutine,channel

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
 PackageMainImport("FMT")funcsum (values []int, ResultchanChan int) {sum: =0     for_, Value: =RangeValues {sum + = value} Resultchan <-sum//Writes the result of the operation to the channel}funcMain () {values: = []int{1,2,3,4,5,6,7,8,9,Ten}//defines and initializes a numeric array of size 10Resultchan: = Make(Chan int,2)//define channel with size 2    GoSUM (values[:Len(values)/2], Resultchan)//Open 1 computes the second half of the array and writes the result to the channel blocked    GoSUM (values[Len(values)/2:], Resultchan)//Open 2 computes the first half of the array and writes the results to the channel blockedSUM1, sum2: = <-resultchan, <-resultchan//read out the blocked data in the ChennelFmt. Println ("Result:", Sum1, sum2, sum1+sum2)}
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.