18 lines
334 B
Go
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()
|
|
}
|