How to Copy an Array into Another Array in Golang?

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how
// to copy an array by value
package 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)
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to
// copy an array by reference
package 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)
}

chevron_right


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]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.