package group import ( "context" "sync" "golang.org/x/sync/errgroup" ) func RunGroup[T any](ctx context.Context, maxRuntimeGoroutines int, collection []T, process func(T) error) error { eg, ctx := errgroup.WithContext(ctx) eg.SetLimit(maxRuntimeGoroutines) taskCh := make(chan T) eg.Go(func() error { defer close(taskCh) for _, c := range collection { select { case <-ctx.Done(): return nil case taskCh <- c: } } return nil }) for task := range taskCh { task := task eg.Go(func() error { return process(task) }) } return eg.Wait() }