Which fix removes the bug?
src := []int{1, 2, 3}
dst := make([]int, 0, len(src))
copy(dst, src)
fmt.Println(dst) // expected [1 2 3]
- Swap the args:
copy(src, dst) - Raise the cap to
len(src)+1 - Use
make([]int, len(src)) - Pass a pointer:
copy(&dst, src)
copy moves min(len(dst), len(src)) elements and ignores capacity. dst has length 0, so nothing is copied; give it length len(src).
Play Problem Solving ▸