Copy an Array by Value and Reference into Another Array in Golang


In Golang, arrays are fixed-size data structures that hold a collection of values of the same type. In some cases, it may be necessary to copy an array to another array either by value or by reference. In this article, we will explore how to copy an array in Golang both by value and by reference.

Copying an Array by Value in Golang

In Golang, when you assign an array to another variable or pass it as a parameter to a function, it is copied by value. This means that any changes made to the copied array will not affect the original array. To copy an array by value, you can simply assign it to a new variable.

Example

Here's an example of copying an array by value in Golang −

package main

import "fmt"

func main() {
   arr1 := [3]int{1, 2, 3}
   arr2 := arr1 // copying arr1 by value to arr2
   arr2[0] = 0  // modifying the first element of arr2

   fmt.Println(arr1) // prints [1 2 3]
   fmt.Println(arr2) // prints [0 2 3]
}

Output

[1 2 3]
[0 2 3]

In the above example, we created an array arr1 with three elements and assigned it to arr2 by value. Then, we modified the first element of arr2 to 0. However, this modification did not affect arr1 as it was copied by value.

Copying an Array by Reference in Golang

In Golang, arrays are copied by reference when they are passed to a function as a slice. A slice is a dynamically-sized, flexible view into the elements of an array. When you pass an array to a function as a slice, it is passed by reference, which means that any changes made to the slice will also affect the original array.

Example

Here's an example of copying an array by reference in Golang −

package main

import "fmt"

func modifySlice(s []int) {
   s[0] = 0 // modifying the first element of s
}

func main() {
   arr1 := [3]int{1, 2, 3}
   slice1 := arr1[:] // creating a slice from arr1
   modifySlice(slice1) // passing slice1 to modifySlice function

   fmt.Println(arr1) // prints [0 2 3]
   fmt.Println(slice1) // prints [0 2 3]
}

Output

[0 2 3]
[0 2 3]

In the above example, we created an array arr1 with three elements and created a slice slice1 from it. We then passed slice1 to the modifySlice function, which modified the first element of slice1 to 0. Since slice1 was created from arr1 by reference, this modification also affected arr1.

Copying Part of an Array in Golang

In Golang, you can also copy part of an array to another array or slice using the copy function. The copy function takes two arguments: the destination array or slice and the source array or slice. It then copies the elements from the source to the destination.

Example

Here's an example of copying part of an array in Golang −

package main

import "fmt"

func main() {
   arr1 := [5]int{1, 2, 3, 4, 5}
   arr2 := [3]int{} // creating an empty array of length 3
   slice1 := arr1[1:4] // creating a slice from arr1

   copy(arr2[:], slice1)
   fmt.Println("arr1: ", arr1)
   fmt.Println("arr2: ", arr2)
   fmt.Println("slice1: ", slice1)
}

Output

arr1:  [1 2 3 4 5]
arr2:  [2 3 4]
slice1:  [2 3 4]

In this example, we first create an array arr1 with 5 elements. Then we create an empty array arr2 of length 3. We also create a slice slice1 from arr1 containing elements from index 1 to 3 (excluding the element at index 3).

We then use the copy() function to copy slice1 into arr2. Since arr2 has a length of 3, only the first 3 elements of slice1 are copied into it.

Finally, we print the contents of arr1, arr2, and slice1 to verify that the copy was successful.

In the output, we can see that arr2 contains the elements [2 3 4], which are the elements copied from slice1.

Conclusion

Arrays are an important data structure in Golang and knowing how to copy them by value and reference is essential for any developer working with arrays. It is important to understand the difference between copying an array by value and copying by reference, as they have different implications and use cases. Copying an array by value creates a new copy of the array, whereas copying by reference creates a new reference to the same underlying array. Additionally, developers should also be familiar with how to copy parts of an array into another array using the built-in copy() function. With these techniques, developers can effectively work with arrays in Golang and create efficient and reliable code.

Updated on: 07-Apr-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements