Back to Lo

Core Fromanyslice

docs/data/core-fromanyslice.md

1.53.0630 B
Original Source

Converts a slice of empty interface values back to a typed slice. Returns the converted slice and a boolean indicating success. All elements must be of the target type.

go
data := []any{1, 2, 3}
result, ok := lo.FromAnySlice[int](data)
// []int{1, 2, 3}, true

data = []any{"a", "b", "c"}
result, ok = lo.FromAnySlice[string](data)
// []string{"a", "b", "c"}, true

data = []any{1, "b", 3} // mixed types
result, ok = lo.FromAnySlice[int](data)
// []int{}, false (conversion failed due to string element)

data = []any{} // empty slice
result, ok = lo.FromAnySlice[int](data)
// []int{}, true (empty slice always succeeds)