diff --git a/slices/map.go b/slices/map.go index c54de57..a1bce4e 100644 --- a/slices/map.go +++ b/slices/map.go @@ -7,6 +7,10 @@ import "context" // MapWithContextError transforms a slice of a given type to a new slice of a // different type using a context and can return an error. func MapWithContextError[T, K any](ctx context.Context, ss []T, fn func(context.Context, T) (K, error)) ([]K, error) { + if ss == nil { + return nil, nil + } + var res = make([]K, len(ss)) for i, v := range ss { diff --git a/slices/map_test.go b/slices/map_test.go index f2939e5..a686d13 100644 --- a/slices/map_test.go +++ b/slices/map_test.go @@ -41,6 +41,12 @@ func TestMapWithContextError(t *testing.T) { }, want: []int64{1, 22}, }, + { + name: "nil", + fn: func(context.Context, string) (int64, error) { + return 0, nil + }, + }, } { t.Run(tt.name, func(t *testing.T) { got, err := MapWithContextError(ctx, tt.in, tt.fn)