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