找到原因了,是切片内存自动扩容和缩容的坑:
```
// The append built-in function appends elements to the end of a slice. If
// it has sufficient capacity, the destination is resliced to accommodate the
// new elements. If it does not, a new underlying array will be allocated.
// Append returns the updated slice. It is therefore necessary to store the
// result of append, often in the variable holding the slice itself:
// slice = append(slice, elem1, elem2)
// slice = append(slice, anotherSlice...)
// As a special case, it is legal to append a string to a byte slice, like this:
// slice = append([]byte("hello "), "world"...)
func append(slice []Type, elems ...Type) []Type
```
当 cap 不够的时候会自动扩容,当多次执行`track = track[:len(track)-1]`这样的操作之切片又会自动缩容,每次扩容和缩容都会导致底层数组的内存重新申请,每两次内存重新申请之前的 append 的值就会一样,增加日志可以验证:
```
Cap Before: 1, track: [5]
Cap Before: 2, track: [5 4]
Cap Before: 4, track: [5 4 6]
Cap Before: 4, track: [5 4 6 2]
Cap After: 4, track: [5 4 6]
Cap After: 4, track: [5 4]
Cap Before: 4, track: [5 4 2]
Cap Before: 4, track: [5 4 2 6]
Cap After: 4, track: [5 4 2]
Cap After: 4, track: [5 4]
Cap After: 2, track: [5]
Cap Before: 2, track: [5 6]
Cap Before: 4, track: [5 6 4]
Cap Before: 4, track: [5 6 4 2]
Cap After: 4, track: [5 6 4]
Cap After: 4, track: [5 6]
Cap Before: 4, track: [5 6 2]
Cap Before: 4, track: [5 6 2 4]
Cap After: 4, track: [5 6 2]
Cap After: 4, track: [5 6]
Cap After: 2, track: [5]
Cap Before: 2, track: [5 2]
Cap Before: 4, track: [5 2 4]
Cap Before: 4, track: [5 2 4 6]
Cap After: 4, track: [5 2 4]
Cap After: 4, track: [5 2]
Cap Before: 4, track: [5 2 6]
Cap Before: 4, track: [5 2 6 4]
Cap After: 4, track: [5 2 6]
Cap After: 4, track: [5 2]
Cap After: 2, track: [5]
Cap After: 1, track: []
Cap Before: 1, track: [4]
Cap Before: 2, track: [4 5]
Cap Before: 4, track: [4 5 6]
Cap Before: 4, track: [4 5 6 2]
Cap After: 4, track: [4 5 6]
Cap After: 4, track: [4 5]
Cap Before: 4, track: [4 5 2]
Cap Before: 4, track: [4 5 2 6]
Cap After: 4, track: [4 5 2]
Cap After: 4, track: [4 5]
Cap After: 2, track: [4]
Cap Before: 2, track: [4 6]
Cap Before: 4, track: [4 6 5]
Cap Before: 4, track: [4 6 5 2]
Cap After: 4, track: [4 6 5]
Cap After: 4, track: [4 6]
Cap Before: 4, track: [4 6 2]
Cap Before: 4, track: [4 6 2 5]
Cap After: 4, track: [4 6 2]
Cap After: 4, track: [4 6]
Cap After: 2, track: [4]
Cap Before: 2, track: [4 2]
Cap Before: 4, track: [4 2 5]
Cap Before: 4, track: [4 2 5 6]
Cap After: 4, track: [4 2 5]
Cap After: 4, track: [4 2]
Cap Before: 4, track: [4 2 6]
Cap Before: 4, track: [4 2 6 5]
Cap After: 4, track: [4 2 6]
Cap After: 4, track: [4 2]
Cap After: 2, track: [4]
Cap After: 1, track: []
Cap Before: 1, track: [6]
Cap Before: 2, track: [6 5]
Cap Before: 4, track: [6 5 4]
Cap Before: 4, track: [6 5 4 2]
Cap After: 4, track: [6 5 4]
Cap After: 4, track: [6 5]
Cap Before: 4, track: [6 5 2]
Cap Before: 4, track: [6 5 2 4]
Cap After: 4, track: [6 5 2]
Cap After: 4, track: [6 5]
Cap After: 2, track: [6]
Cap Before: 2, track: [6 4]
Cap Before: 4, track: [6 4 5]
Cap Before: 4, track: [6 4 5 2]
Cap After: 4, track: [6 4 5]
Cap After: 4, track: [6 4]
Cap Before: 4, track: [6 4 2]
Cap Before: 4, track: [6 4 2 5]
Cap After: 4, track: [6 4 2]
Cap After: 4, track: [6 4]
Cap After: 2, track: [6]
Cap Before: 2, track: [6 2]
Cap Before: 4, track: [6 2 5]
Cap Before: 4, track: [6 2 5 4]
Cap After: 4, track: [6 2 5]
Cap After: 4, track: [6 2]
Cap Before: 4, track: [6 2 4]
Cap Before: 4, track: [6 2 4 5]
Cap After: 4, track: [6 2 4]
Cap After: 4, track: [6 2]
Cap After: 2, track: [6]
Cap After: 1, track: []
Cap Before: 1, track: [2]
Cap Before: 2, track: [2 5]
Cap Before: 4, track: [2 5 4]
Cap Before: 4, track: [2 5 4 6]
Cap After: 4, track: [2 5 4]
Cap After: 4, track: [2 5]
Cap Before: 4, track: [2 5 6]
Cap Before: 4, track: [2 5 6 4]
Cap After: 4, track: [2 5 6]
Cap After: 4, track: [2 5]
Cap After: 2, track: [2]
Cap Before: 2, track: [2 4]
Cap Before: 4, track: [2 4 5]
Cap Before: 4, track: [2 4 5 6]
Cap After: 4, track: [2 4 5]
Cap After: 4, track: [2 4]
Cap Before: 4, track: [2 4 6]
Cap Before: 4, track: [2 4 6 5]
Cap After: 4, track: [2 4 6]
Cap After: 4, track: [2 4]
Cap After: 2, track: [2]
Cap Before: 2, track: [2 6]
Cap Before: 4, track: [2 6 5]
Cap Before: 4, track: [2 6 5 4]
Cap After: 4, track: [2 6 5]
Cap After: 4, track: [2 6]
Cap Before: 4, track: [2 6 4]
Cap Before: 4, track: [2 6 4 5]
Cap After: 4, track: [2 6 4]
Cap After: 4, track: [2 6]
Cap After: 2, track: [2]
Cap After: 1, track: []
```