Welcome To Golang By Example

Copy an array or slice in Go (Golang)

Overview

Array are value types in go while slice is a reference type. Hence there is a difference in the way how an array or slice can be copied to another array or slice respectively.

Copy an array

As mentioned above, an array is value types in go. So an array variable name is not a pointer to the first element, in fact, it denotes the entire array. A copy of the array will be automatically created when

Let’s see above point with an example

package main

import "fmt"

func main() {
	sample1 := [2]string{"a", "b"}
	fmt.Printf("Sample1 Before: %v\n", sample1)
	sample2 := sample1
	sample2[1] = "c"
	fmt.Printf("Sample1 After assignment: %v\n", sample1)
	fmt.Printf("Sample2: %v\n", sample2)
	test(sample1)
	fmt.Printf("Sample1 After Test Function Call: %v\n", sample1)
}
func test(sample [2]string) {
	sample[0] = "d"
	fmt.Printf("Sample in Test function: %v\n", sample)
}

Output

Sample1 Before: [a b]
Sample1 After assignment: [a b]
Sample2: [a c]
Sample in Test function: [d b]
Sample1 After Test Function Call: [a b]

In above example,

Copy a slice

go builtin package provides a copy function that can be used to copy a slice. Below is the signature of this function. It returns the number of elements copied.

func copy(dst, src []Type) int

There are two cases to be considered while using the copy function:

Basically the number of elements copied is minimum of length of (src, dst). 

Also to note that once the copy is done then any change in dst will not reflect in src and vice versa

package main

import "fmt"

func main() {
    src := []int{1, 2, 3, 4, 5}
    dst := make([]int, 5)

    numberOfElementsCopied := copy(dst, src)
    fmt.Printf("Number Of Elements Copied: %d\n", numberOfElementsCopied)
    fmt.Printf("dst: %v\n", dst)
    fmt.Printf("src: %v\n", src)

    //After changing numbers2
    dst[0] = 10
    fmt.Println("\nAfter changing dst")
    fmt.Printf("dst: %v\n", dst)
    fmt.Printf("src: %v\n", src)
}

Output

Number Of Elements Copied: 5
dst: [1 2 3 4 5]
src: [1 2 3 4 5]

After changing dst
dst: [10 2 3 4 5]
src: [1 2 3 4 5]