Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- )
- const nBatchSize int = 2
- func summ(a []int, r chan int) {
- summ := 0
- for _, v := range a {
- summ += v
- }
- r <- summ
- }
- func main() {
- a := make([]int, 0, 100)
- for i := 0; i < 100; i++ {
- a = append(a, i)
- }
- r := make(chan int)
- nBatch := int(len(a) / nBatchSize)
- for j := 0; j < nBatch; j++ {
- go summ(a[j*nBatchSize:(j+1)*nBatchSize], r)
- }
- total := 0
- for i := 0; i < nBatch; i++ {
- summ := <-r
- total += summ
- }
- fmt.Println("Total =", total)
- }
Advertisement
Add Comment
Please, Sign In to add comment