Skip to content

Instantly share code, notes, and snippets.

@KhodeN
Created March 20, 2018 07:50
Show Gist options
  • Save KhodeN/f8be2f82c8ad1d5f7f1e562b2372eb72 to your computer and use it in GitHub Desktop.
Save KhodeN/f8be2f82c8ad1d5f7f1e562b2372eb72 to your computer and use it in GitHub Desktop.
batch_paraller.go
package main
import (
"fmt"
"time"
)
func main() {
input := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
inChan := make(chan int, 3)
outChan := make(chan int, len(input))
for i := 0; i < 3; i++ {
go run(inChan, outChan)
}
for _, v := range input {
inChan <- v
fmt.Printf("%d ", v)
}
close(inChan)
for range input {
fmt.Printf("\n%d", <-outChan)
}
}
func run(i <-chan int, o chan<- int) {
for {
select {
case v, ok := <-i:
if !ok {
break
}
time.Sleep(time.Second * time.Duration(v/4))
res := v * v
o <- res
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment