Last active
March 13, 2017 15:41
-
-
Save vkobel/0374e7a333f6155602dbf8f9177bf039 to your computer and use it in GitHub Desktop.
Separate an array into chunks and process them asynchronously. Then returns the results using go channels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
const processes int = 4 | |
func Chunkate(data []int, fn func([]int, chan int)) []int { | |
var result []int | |
chunkSize := len(data) / processes | |
c := make(chan int) | |
for i := 0; i < len(data); i += chunkSize { | |
end := i + chunkSize | |
if end > len(data) { | |
end = len(data) | |
} | |
go fn(data[i:end], c) | |
} | |
for i := 0; i < len(data); i++ { | |
select { | |
case res := <-c: | |
result = append(result, res) | |
} | |
} | |
return result | |
} | |
func process(data []int, c chan int) { | |
time.Sleep(2000 * time.Millisecond) | |
for _, d := range data { | |
c <- d | |
} | |
} | |
func main() { | |
data := make([]int, 80) | |
for i := 0; i < 80; i++ { | |
data[i] = i | |
} | |
res := Chunkate(data, process) | |
fmt.Println(res) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment