A classic for loop evaluates its condition during every iteration. That differs from a range loop, whose input expression is evaluated once and copied before iteration begins. section 4.2 section 4.7
A growing slice can prevent termination
With s := []int{0, 1, 2}, a loop such as `for i := 0; i < len(s); i++ { s = append(s, 10) }` keeps increasing len(s) while testing it, so the termination condition is never reached. A range loop over the same slice completes after the original three iterations because it iterates over a three-element temporary copy. section 4.2
Use the loop form that matches the operation:
- Use a classic for loop with an index when updating selected elements or controlling iteration explicitly. section 4.1.2
- Remember that a range value is a copy: changing a copied struct does not change the slice element; update through its index instead. section 4.1.2 section 4.7
- When deferring cleanup inside a loop, add a surrounding function so defer runs at each iteration rather than when the outer function returns. section 4.6
s := []int{0, 1, 2}
for i := 0; i < len(s); i++ {
s = append(s, 10)
}
Inspect the classic loop whose condition depends on the continually growing slice length. section 4.2


























