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
package main
import "fmt"
func main() {
my_arr1 := [ 5 ] string { "Scala" , "Perl" , "Java" , "Python" , "Go" }
my_arr2 := my_arr1
fmt.Println( "Array_1: " , my_arr1)
fmt.Println( "Array_2:" , my_arr2)
my_arr1[ 0 ] = "C++"
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
package main
import "fmt"
func
main()
{
my_arr1:
= [6] int { 12, 456, 67, 65, 34, 34 }
my_arr2 : = &my_arr1
fmt.Println( "Array_1: " , my_arr1)
fmt.Println( "Array_2:" , *my_arr2)
my_arr1[5]
= 1000000
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
02 Mar, 2023
Like Article
Save Article