Go gotcha: What happened to my copy?

Why does the copy disappear?

var src, dst []int
src = []int{1, 2, 3}
copy(dst, src) // Copy elements to dst from src.
fmt.Println("dst:", dst)
dst: []

The number of elements copied by the copy function is the minimum of len(dst) and len(src). To make a full copy, you must allocate a big enough destination slice:

dst = make([]int, len(src))
n := copy(dst, src)
fmt.Println("dst:", dst, "(copied", n, "numbers)")
dst: [1 2 3] (copied 3 numbers)

The return value of the copy function is the number of elements copied.

You could also use the append function to make a copy by appending to a nil slice:

dst = append([]int(nil), src...)
fmt.Println("dst:", dst)
dst: [1 2 3]

See Copy function explained for more about the built-in copy function in Go.


Be the first to comment!