Filter a slice in place without allocation:
func filter(s []int, keep func(int) bool) []int {
n := 0
for _, v := range s {
if keep(v) {
s[n] = v
n++
}
}
return s[:n]
}
This overwrites the slice with kept elements. Efficient when you can modify the original.