Skip to content

All

Executes a function for each item in a slice concurrently. Uses a Group internally.

Signature

go
func All[T any](ctx context.Context, items []T, fn func(ctx context.Context, item T) error, opts ...GroupOption) error

Type Parameters

ParameterDescription
TThe type of each item in the slice.

Parameters

ParameterTypeDescription
ctxcontext.ContextParent context.
items[]TSlice of items to iterate over.
fnfunc(ctx context.Context, item T) errorFunction to execute for each item.
opts...GroupOptionOptions passed to the underlying NewGroup call.

Return Value

Returns nil if all invocations succeed. Otherwise returns all errors via errors.Join.

If items is empty, returns nil immediately without creating a group.

Options

Naming

OptionDescription
WithName(name)Custom metric/tracing label. Default: "gofuncy.all"

Resilience

OptionDescription
WithTimeout(d)Per-invocation timeout. Each retry attempt gets a fresh deadline.
WithRetry(n, opts...)Automatic retry with configurable backoff.
WithCircuitBreaker(cb)Fail fast on broken dependencies. Stateful — share across calls.
WithFallback(fn, opts...)Called when the operation fails. Return nil to suppress the error.

Telemetry

OptionDefaultDescription
WithoutTracing()onDisable span creation.
WithDetachedTrace()variesRoot span linked to parent instead of child span. Default for Go/Start/StartWithReady/StartWithStop/GoWithCancel.
WithChildTrace()variesForce child span. Default for Do/Wait/NewGroup.
WithoutStartedCounter()onDisable started counter.
WithoutErrorCounter()onDisable error counter.
WithoutActiveUpDownCounter()onDisable active counter.
WithDurationHistogram()offEnable duration histogram.
WithMeterProvider(mp)globalCustom OTel meter provider.
WithTracerProvider(tp)globalCustom OTel tracer provider.

Concurrency

OptionDescription
WithLimiter(sem)Shared *semaphore.Weighted for cross-callsite concurrency control.

Middleware

OptionDescription
WithMiddleware(m...)Append custom middleware. Applied after resilience, before telemetry.
WithLogger(l)Custom *slog.Logger for error reporting.
WithStallThreshold(d)Log a warning if the goroutine runs longer than d.
WithStallHandler(h)Custom callback for stall detection.

Group-Only

OptionDescription
WithLimit(n)Max concurrent functions in this group.
WithFailFast()Cancel remaining functions on first error.

Example

go
package main

import (
	"context"
	"fmt"

	"github.com/foomo/gofuncy"
)

func main() {
	ctx := context.Background()

	urls := []string{
		"https://api.example.com/users",
		"https://api.example.com/orders",
		"https://api.example.com/products",
	}

	err := gofuncy.All(ctx, urls, func(ctx context.Context, url string) error {
		fmt.Println("fetching", url)
		// fetch(ctx, url)
		return nil
	},
		gofuncy.WithLimit(2), // at most 2 concurrent fetches
	)
	if err != nil {
		fmt.Println("errors:", err)
	}
}

TIP

All is a convenience wrapper around NewGroup + Add + Wait. If you need per-item options or want to add functions dynamically, use Group directly.