collection/each.go
2022-04-07 21:36:40 +03:00

23 lines
393 B
Go

package collection
import "sync"
func EachSync[T any](collection []T, cb func(item T, idx int)) {
for i, v := range collection {
cb(v, i)
}
}
func Each[T any](collection []T, cb func(item T, idx int)) {
wg := sync.WaitGroup{}
wg.Add(len(collection))
for i, v := range collection {
func(i int, v T) {
go func() {
defer wg.Done()
cb(v, i)
}()
}(i, v)
}
wg.Wait()
}