Skip to content

Map ​

Transforms items concurrently while preserving input order. Uses a Group internally.

Signature ​

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

Type Parameters ​

ParameterDescription
TInput item type.
ROutput result type.

Parameters ​

ParameterTypeDescription
ctxcontext.ContextParent context.
items[]TSlice of items to transform.
fnfunc(ctx context.Context, item T) (R, error)Transformation function.
opts...GroupOptionOptions passed to the underlying NewGroup call.

Return Values ​

ValueDescription
[]RResults in the same order as the input items. On error, elements at failing indices contain the zero value of R.
errornil if all transformations succeed. Otherwise all errors via errors.Join.

If items is empty, returns (nil, nil) immediately.

Options ​

Naming ​

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

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"
	"strings"

	"github.com/foomo/gofuncy"
)

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

	ids := []int{1, 2, 3, 4, 5}

	users, err := gofuncy.Map(ctx, ids, func(ctx context.Context, id int) (string, error) {
		// Simulate fetching a user by ID
		return fmt.Sprintf("user-%d", id), nil
	},
		gofuncy.WithLimit(3),
	)
	if err != nil {
		fmt.Println("errors:", err)
	}

	fmt.Println(strings.Join(users, ", "))
	// Output: user-1, user-2, user-3, user-4, user-5
}

WARNING

Results are stored by index. If some transformations fail and you did not use WithFailFast, the result slice will contain zero values at the indices of failed items. Always check the returned error.

TIP

Map preserves order by storing results at the corresponding index. There is no need to sort or reorder results after the call.