Build a new collection from elements matching a condition:
nums := []int{1, 2, 3, 4, 5, 6}
var evens []int
for _, n := range nums {
if n%2 == 0 {
evens = append(evens, n)
}
}
This creates a slice containing only even numbers. The pattern starts with an empty slice and appends matching elements. You'll use this often for data processing.