OneBug This week's bugs
Spot the bug Go
1
// keepPositive returns the positive numbers, preserving order.
2
func keepPositive(nums []int) []int {
3
	out := nums[:0]
4
	for _, n := range nums {
5
		if n > 0 {
6
			out = append(out, n)
7
		}
8
	}
9
	return out
10
}
11
 
12
func main() {
13
	data := []int{3, -1, 4, -1, 5}
14
	fmt.Println(keepPositive(data)) // [3 4 5]
15
	fmt.Println(data)               // [3 4 5 -1 5]  (!)
16
}

Read the full write-up for this bug (spoilers)