Skip to content

Instantly share code, notes, and snippets.

@mikeplavsky
Created August 31, 2020 21:27
Show Gist options
  • Save mikeplavsky/27bff1c4a62a541fd0d191a5d1fbfa70 to your computer and use it in GitHub Desktop.
Save mikeplavsky/27bff1c4a62a541fd0d191a5d1fbfa70 to your computer and use it in GitHub Desktop.
func runner(tasks []int, workers int, f func(int) int) {
in, out := make(chan int), make(chan int)
for i := 0; i < workers; i++ {
go func() {
for {
out <- f(<-in)
}
}()
}
for _, v := range tasks {
in <- v
}
var res []int
for i := 0; i < len(tasks); i++ {
res = append(res, <-out)
}
fmt.Println("Done!\n", res)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment