Golang copy function
last modified May 8, 2025
This tutorial explains how to use the copy
built-in function in Go.
We'll cover slice copying basics with practical examples of efficient data copying.
The copy function copies elements from a source slice into a destination slice. It returns the number of elements copied, which is the minimum of the source and destination lengths.
In Go, copy
is used to efficiently transfer data between slices or
arrays. It handles overlapping slices correctly and is more efficient than manual
looping for copying data.
Basic slice copying example
The simplest use of copy
transfers data between two slices. This
example demonstrates basic slice copying.
Note: The destination slice must have sufficient capacity.
package main import "fmt" func main() { src := []int{1, 2, 3, 4, 5} dst := make([]int, len(src)) n := copy(dst, src) fmt.Println("Copied", n, "elements") fmt.Println("Source:", src) fmt.Println("Destination:", dst) }
The copy function copies all elements from src to dst. The returned value n equals the length of src since dst has the same length.
Partial slice copying
We can copy only a portion of a slice by using slice expressions. This example shows partial copying between slices.
package main import "fmt" func main() { src := []string{"apple", "banana", "cherry", "date", "elderberry"} dst := make([]string, 3) n := copy(dst, src[1:4]) fmt.Println("Copied", n, "elements") fmt.Println("Source:", src) fmt.Println("Destination:", dst) }
The code copies elements 1 through 3 from src to dst. Only 3 elements are copied because dst has length 3.
Copying between different types
The copy function can work between different types if they have identical underlying types. This example demonstrates copying between byte and uint8 slices.
package main import "fmt" func main() { src := []byte{'G', 'o', 'l', 'a', 'n', 'g'} dst := make([]uint8, len(src)) n := copy(dst, src) fmt.Println("Copied", n, "elements") fmt.Printf("Source: %v\n", src) fmt.Printf("Destination: %v\n", dst) }
Byte and uint8 are identical types in Go, so copy works between them. The function copies all elements successfully.
Copying with overlapping slices
The copy function handles overlapping slices correctly. This example shows how copy can be used to shift elements within a single slice.
package main import "fmt" func main() { data := []int{1, 2, 3, 4, 5, 6, 7, 8} // Shift elements left by 2 positions n := copy(data, data[2:]) // Truncate the slice data = data[:n] fmt.Println("Copied", n, "elements") fmt.Println("Result:", data) }
The copy operation shifts elements left within the same slice. The final slice is truncated to remove the leftover elements at the end.
Copying from arrays to slices
The copy function can copy data from arrays to slices. This example demonstrates copying from a fixed-size array to a slice.
package main import "fmt" func main() { src := [5]int{10, 20, 30, 40, 50} dst := make([]int, 3) n := copy(dst, src[:]) fmt.Println("Copied", n, "elements") fmt.Println("Source array:", src) fmt.Println("Destination slice:", dst) }
The array is converted to a slice using the [:] operator before copying. Only 3 elements are copied because dst has length 3.
Source
This tutorial covered the copy
function in Go with practical
examples of efficient data copying between slices and arrays.
Author
List all Golang tutorials.