aboutsummaryrefslogtreecommitdiff
path: root/each.go
blob: d1150542de3f5e3aa46fe4f676c2901a86bd4760 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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()
}