The copy() Function

Let’s learn about the copy() function in Go.

About the copy() function

Go offers the copy() function for copying an existing array to a slice or an existing slice to another slice. However, the use of copy() can be tricky because the destination slice is not automatically expanded if the source slice is bigger than the destination slice. Additionally, if the destination slice is bigger than the source slice, then copy() does not empty the elements from the destination slice that did not get copied.

This is better illustrated in the figure that follows.

Get hands-on with 1200+ tech skills courses.