I need to make a copy of a slice in go and reading the docs there is a copy function at my disposal.

The copy built-in function copies elements from a source slice into a
destination slice. (As a special case, it also will copy bytes from a
string to a slice of bytes.) The source and destination may overlap.
Copy returns the number of elements copied, which will be the minimum
of len(src) and len(dst).

@Bob I just checked and the documentation has bee updated to contain this information. I also updated the answer to mention this.
– iczaFeb 17 '16 at 6:50

1

To summarise, copy doesn't contain logic for growing the destination slice if the destination slice is too small, but there is another built-in function that does: append While in this example it's better just to allocate the right sized slice in the first place, append can be used when you already have a slice and want to grow it by adding elements to the end.
– thomasrutterAug 2 '17 at 5:35

1

But why do I have to create a bounded size slice when copying an unbounded sized slice?
– AlexFeb 20 at 21:14

the catch here is that in real-world examples, which are much larger, append will allocate excess memory -- unless this array is later filled to capacity by some further processing -- because it is designed for efficient reallocation over repeated calls. play.golang.org/p/5_6618xnXn observe that cap(x) increases to 12, not 10. now look what happens when 1 value is added to 1048576 values play.golang.org/p/nz32JPehhl the capacity jumps by 2048 slots to 1050624, to accommodate only one additional value.
– j. andrew shustaDec 5 '17 at 15:02

append should be used in cases where the array will be increased by repeated calls, as it will optimistically allocate excess capacity in anticipation of this. copy should be used once per input array in cases where the result array should be created to exact size, and is not reallocated from again. play.golang.org/p/0kviwKmGzx you didn't share the benchmark code that produced those results so i can't confirm or deny its validity, but it overlooks this more important aspect.
– j. andrew shustaDec 5 '17 at 15:23

The function copy copies slice elements from a source src to a
destination dst and returns the number of elements copied. Both
arguments must have identical element type T and must be assignable to
a slice of type []T. The number of elements copied is the minimum of
len(src) and len(dst). As a special case, copy also accepts a
destination argument assignable to type []byte with a source argument
of a string type. This form copies the bytes from the string into the
byte slice.