isbasov

new

Feb 4th, 2021
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.53 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5. )
  6.  
  7. const nBatchSize int = 2
  8.  
  9. func summ(a []int, r chan int) {
  10.  
  11.     summ := 0
  12.     for _, v := range a {
  13.         summ += v
  14.     }
  15.  
  16.     r <- summ
  17. }
  18.  
  19. func main() {
  20.  
  21.     a := make([]int, 0, 100)
  22.     for i := 0; i < 100; i++ {
  23.         a = append(a, i)
  24.     }
  25.  
  26.     r := make(chan int)
  27.     nBatch := int(len(a) / nBatchSize)
  28.  
  29.     for j := 0; j < nBatch; j++ {
  30.         go summ(a[j*nBatchSize:(j+1)*nBatchSize], r)
  31.     }
  32.  
  33.     total := 0
  34.     for i := 0; i < nBatch; i++ {
  35.         summ := <-r
  36.         total += summ
  37.     }
  38.  
  39.     fmt.Println("Total =", total)
  40.  
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment