Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.31 KB | None | 0 0
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. func sum(s []int, c chan int) {
  6. sum := 0
  7. for _, v := range s {
  8. sum += v
  9. }
  10. c <- sum // send sum to c
  11. }
  12.  
  13. func main() {
  14. s := []int{7, 2, 8, -9, 4, 0}
  15.  
  16. c := make(chan int)
  17. go sum(s[:len(s)/2], c)
  18. go sum(s[len(s)/2:], c)
  19. x, y := <-c, <-c // receive from c
  20.  
  21. fmt.Println(x, y, x+y)
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement