workers/workers.go
2022-08-16 02:11:29 +02:00

18 lines
334 B
Go

package workers
import "sync"
// Channel launches n workers to perform an action for every item in the input channel
func Channel[T any](n int, ch chan T, handler func(T)) {
var wg sync.WaitGroup
wg.Add(n)
for i := 0; i < n; i++ {
go func() {
for item := range ch {
handler(item)
}
wg.Done()
}()
}
wg.Wait()
}