AppDividend
Latest Code Tutorials

Golang: How To Copy Array Into Another Array

0

Array in Go is a fixed-size collection of elements of the same type. The elements of the array are stored sequentially and can be accessed using their index. Golang does not provide a specific inbuilt function to copy one array into another array. But we can create a copy of an array by simply assigning an array to a new variable by value or by reference.

How To Copy Array Into Another Array in Golang

If we create a copy of an array by value and made some changes in the values of the original array, then it will not reflect in the copy of that array.

If we create a copy of an array by reference and made some changes in the values of the original array, then it will reflect in the copy of that array.

Syntax

// creating a copy of an array by value
arrByVal := arr

// Creating a copy of an array by reference
arrByRef := &arr

Let’s take a brief example in which we create an array then copy that array into another array using by value and by reference.

package main

import "fmt"

func main() {

	arr := [4]string{"Italy", "Spain", "Germany", "France"}
	arrByVal := arr  // data is passed by value
	arrByRef := &arr // data is passed by refrence

	fmt.Printf("arr: %v\n", arr)
	fmt.Printf("arrByVal: %v\n", arrByVal)
	fmt.Printf("*arrByRef: %v\n", *arrByRef)

	arr[3] = "USA"
	fmt.Printf("---After changing the orignal arr array---	\n")

	fmt.Printf("arr: %v\n", arr)
	fmt.Printf("arrByVal: %v\n", arrByVal)
	fmt.Printf("*arrByRef: %v\n", *arrByRef)
}

Output

arr: [Italy Spain Germany France]
arrByVal: [Italy Spain Germany France]
*arrByRef: [Italy Spain Germany France]
---After changing the orignal arr array---
arr: [Italy Spain Germany USA]
arrByVal: [Italy Spain Germany France]
*arrByRef: [Italy Spain Germany USA]

Explanation

First, we have defined an array of strings called arr, which consists of four string elements.

Then we have created a copy of the array from arr using by value. That newly created copied array is called arrByVal.

Then we have created another copy of the array from arr using by reference. That newly created copied array is called arrByRef.

So, we have created two copied arrays from an original array.

  1. arrByVal: The array is copied using by value.
  2. arrByRef: The array is copied using by reference.

In the next step, we have modified the original value by replacing France with the USA.

That means now the original array is modified.

Now, let’s see which of the copied array has changed.

So, in the next step again, we have printed the two copied arrays. 1) arrayByVal 2) arrayByRef

From the output, we can see that copied by value array is not changed, but copied by reference array is changed with the new value.

Is there any built-in function in Golang for copying one array to another array

If you are using Golang slice, then there is one function called copy(), which you can use to copy one array into another array.

See the following code.

package main

import "fmt"

func main() {

	arr := []string{"Italy", "Spain"}
	arrSource := []string{"Germany", "France"}

	copy(arr, arrSource) // data is passed by value

	fmt.Printf("Orignal Array: %v\n", arrSource)
	fmt.Printf("Copied Array: %v\n", arr)
}

Output

go run hello.go
Orignal Array: [Germany France]
Copied Array: [Germany France]

The copy() function will do a shallow copy and not a deep copy. There’s no inbuilt function for doing deep-copy of multi-dimensional arrays.

Conclusion

The first approach for copying an array in Go is the assignment operator =, as it is the case for any other value of any other type.

If you are using an actual array (not something created with make(), then the assignment works perfectly well.

However, if you’re dealing with slices (which I can see how some would be confused by the difference), then you’ll need to use copy() function explicitly, or else you will have a reference type with pointers causing issues.

Finally, our copy array in Golang example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.