-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.go
48 lines (38 loc) · 837 Bytes
/
channel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import "fmt"
func sum(s []int, c chan int) {
// implement a method that sums all elements of `s` slice and
// sends the result to `c` channel
lSum := s[0] + s[1] +s[2]
c <- lSum
}
func main() {
s := []int{1, 5, -2, 7, 2, 8, -9, 4, 0}
c := make(chan int)
//lSlice := s[0:3]
//go sum(lSlice, c)
//
//n := <- c
//
//fmt.Println(n)
for i:=0; i<len(s); i=i+3{
//fmt.Println(i)
lSlice := s[i:i+3]
go sum(lSlice, c)
//fmt.Println(len(s), lSlice, c)
}
//close(c)
totalSum := 0
x := <- c
y := <- c
z := <- c
totalSum = x+y+z
//for v := range c {
// fmt.Println("local sum: %d", v)
// totalSum = totalSum + v
// fmt.Println("hello", totalSum)
//}
fmt.Println("Total sum is %d", totalSum)
// sum `s` slice by taking three elements each time
// print sum of each time and the total result
}