mirror of
https://github.com/ddworken/hishtory.git
synced 2024-11-22 16:24:00 +01:00
7c07236dc0
This reverts commit 632ecc5c81
.
33 lines
505 B
Go
33 lines
505 B
Go
package shared
|
|
|
|
import "sync"
|
|
|
|
func ForEach[T any](arr []T, numThreads int, fn func(T) error) error {
|
|
wg := &sync.WaitGroup{}
|
|
wg.Add(len(arr))
|
|
|
|
limiter := make(chan bool, numThreads)
|
|
|
|
var errors []error
|
|
for _, item := range arr {
|
|
limiter <- true
|
|
go func(x T) {
|
|
defer wg.Done()
|
|
err := fn(x)
|
|
if err != nil {
|
|
errors = append(errors, err)
|
|
}
|
|
<-limiter
|
|
}(item)
|
|
if len(errors) > 0 {
|
|
return errors[0]
|
|
}
|
|
}
|
|
|
|
wg.Wait()
|
|
if len(errors) > 0 {
|
|
return errors[0]
|
|
}
|
|
return nil
|
|
}
|