Back to Lo

Core Toanyslice

docs/data/core-toanyslice.md

1.53.0474 B
Original Source

Converts a typed slice to a slice of empty interface values (any). This is useful when working with heterogeneous data.

go
ints := []int{1, 2, 3}
result := lo.ToAnySlice(ints)
// []any{1, 2, 3}

strings := []string{"a", "b", "c"}
result = lo.ToAnySlice(strings)
// []any{"a", "b", "c"}

custom := []struct{ Name string }{{Name: "Alice"}, {Name: "Bob"}}
result = lo.ToAnySlice(custom)
// []any{struct{ Name string }{Name: "Alice"}, struct{ Name string }{Name: "Bob"}}