Channel channel usage for Go language

Source: Internet
Author: User

This paper describes the use of channel channel in the Go language. Share to everyone for your reference. The specific analysis is as follows:

Channel is a type of pipe that can be sent or received with the channel operator <-.
CH <-V//V is fed into channel ch.
V: = <-ch//is received from CH and assigned to V.
(the arrow is the direction of the data flow.) )
Like map and slice, the channel must be created before it is used:
CH: = make (chan int)
By default, both send and receive are blocked until the other end is ready. This allows the goroutine to synchronize without a definite lock or race variable.

The code is as follows:
Package Mainimport"FMT"func sum (a []int, C Chanint) {sum:=0     for_, V: =range a {sum+=v} c<-sum//send sum to C}func Main () {a:= []int{7,2,8, -9,4,0} C:= Make (chanint) Go sum (A[:len (a)/2], c) Go sum (A[len (a)/2:], c) x, Y:= <-c, <-c//receive from CFmt. Println (x, y, x +y)}

It is hoped that this article will be helpful to everyone's go language programming.

Channel channel usage for Go language

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.