Thursday, December 5, 2024
Google search engine
HomeLanguagesGolangHow to Copy an Array into Another Array in Golang?

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 that 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




// 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)
}


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: 

C




// 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)
}


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]

There are several ways to copy an array into another array in Go. Here are three common methods:

1.Using a loop:

Go




package main
 
import "fmt"
 
func main() {
    originalArray := []int{1, 2, 3, 4, 5}
    copyArray := make([]int, len(originalArray))
 
    for i, value := range originalArray {
        copyArray[i] = value
    }
 
    fmt.Println("Original Array: ", originalArray)
    fmt.Println("Copy Array: ", copyArray)
}


Output:

Original Array: [1 2 3 4 5]
Copy Array: [1 2 3 4 5]

2.Using the copy function:

Go




package main
 
import "fmt"
 
func main() {
    originalArray := []int{1, 2, 3, 4, 5}
    copyArray := make([]int, len(originalArray))
 
    copy(copyArray, originalArray)
 
    fmt.Println("Original Array: ", originalArray)
    fmt.Println("Copy Array: ", copyArray)
}


Output:

Original Array: [1 2 3 4 5]
Copy Array: [1 2 3 4 5]

3. Using the append function:

Go




package main
 
import "fmt"
 
func main() {
    originalArray := []int{1, 2, 3, 4, 5}
    copyArray := make([]int, 0, len(originalArray))
 
    copyArray = append(copyArray, originalArray...)
 
    fmt.Println("Original Array: ", originalArray)
    fmt.Println("Copy Array: ", copyArray)
}


Output:

Original Array: [1 2 3 4 5]
Copy Array: [1 2 3 4 5]

These are the three most common methods for copying an array into another array in Go. The copy function is the most efficient way to copy arrays, but the other methods can be useful in different scenarios.

RELATED ARTICLES

Most Popular

Recent Comments