Gaaker Go

Golang Copy a slice

The built-in function copy can be used to copy a slice.

The following is a complete code example.

package main
import "fmt"

func main() {
	slice := make([]int, 10)

	n := copy(slice, []int{1, 2, 3})
	fmt.Printf("slice: %v, Copied %d elements\n", slice, n)
}

Below is the program output:

slice: [1 2 3 0 0 0 0 0 0 0], Copied 3 elements
< Copy a slice Copy a slice >