diff options
author | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2022-04-07 21:36:40 +0300 |
---|---|---|
committer | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2022-04-07 21:36:40 +0300 |
commit | 9cd6b0fca87679e570fbdd4942e3068feb3439fa (patch) | |
tree | 557bf877d71854480f4171e9dd324cebb5606056 /each.go |
initial
Diffstat (limited to 'each.go')
-rw-r--r-- | each.go | 23 |
1 files changed, 23 insertions, 0 deletions
@@ -0,0 +1,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() +} |