How to Copy an Array into Another Array in Golang? - GeeksforGeeks

26 Aug, 2019
Arrays in Golang or Go programming language is much similar to other programming languages. In the program, sometimes we need to store a collection of data of the same type, like a list of student marks. Such type of collection is stored in a program using an Array. An array is a fixed-length sequence which is used to store homogeneous elements in the memory. Golang does not provide a specific built-in 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.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. And 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. As shown in the below examples:
Syntax:
// creating a copy of an array by value
arr := arr1

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

Let us discuss this concept with the help of the examples:
Example 1:
// Go program to illustrate how// to copy an array by valuepackage main  import "fmt"  func main() {      // Creating and initializing an array    // Using shorthand declaration    my_arr1 := [5]string{"Scala", "Perl", "Java", "Python", "Go"}      // Copying the array into new variable    // Here, the elements are passed by value    my_arr2 := my_arr1      fmt.Println("Array_1: ", my_arr1)    fmt.Println("Array_2:", my_arr2)      my_arr1[0] = "C++"      // Here, when we copy an array    // into another array by value    // then the changes made in original    // array do not reflect in the    // copy of that array    fmt.Println("\nArray_1: ", my_arr1)    fmt.Println("Array_2: ", my_arr2)}
Output:
Array_1:  [Scala Perl Java Python Go]
Array_2: [Scala Perl Java Python Go]

Array_1:  [C++ Perl Java Python Go]
Array_2:  [Scala Perl Java Python Go]

Example 2:
// Go program to illustrate how to// copy an array by referencepackage main  import "fmt"  func main() {      // Creating and initializing an array    // Using shorthand declaration    my_arr1 := [6]int{12, 456, 67, 65, 34, 34}      // Copying the array into new variable    // Here, the elements are passed by reference    my_arr2 := &my_arr1      fmt.Println("Array_1: ", my_arr1)    fmt.Println("Array_2:", *my_arr2)      my_arr1[5] = 1000000      // Here, when we copy an array     // into another array by reference    // then the changes made in original     // array will reflect in the    // copy of that array    fmt.Println("\nArray_1: ", my_arr1)    fmt.Println("Array_2:", *my_arr2)}
Output:
Array_1:  [12 456 67 65 34 34]
Array_2: [12 456 67 65 34 34]

Array_1:  [12 456 67 65 34 1000000]
Array_2: [12 456 67 65 34 1000000]

Golang
Golang-Arrays
Go Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to concatenate two strings in Golang
time.Sleep() Function in Golang With Examples
strings.Contains Function in Golang with Examples
Time Formatting in Golang
strings.Replace() Function in Golang With Examples
fmt.Sprintf() Function in Golang With Examples
Golang Maps
Interfaces in Golang
Inheritance in GoLang
Different Ways to Find the Type of Variable in Golang