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:
// 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:
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]
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]
- Copy an Array by Value and Reference into Another Array in Golang
- How to copy one slice into another slice in Golang?
- How to copy a map to another map in Golang?
- Copy the Sign of Given Number in Golang
- reflect.Copy() Function in Golang with Examples
- io.Copy() Function in Golang with Examples
- How to Copy Struct Type Using Value and Pointer Reference in Golang?
- Converting a string variable into Boolean, Integer or Float type in Golang
- Golang Pointer to an Array as Function Argument
- How to pass an Array to a Function in Golang?
- How to Convert a zero terminated byte array to string in Golang?
- How to iterate over an Array using for loop in Golang?
- Golang Program to Find Largest Element in an Array
- Program to shift all zero to the end of array in Golang
- How to use Array Reverse Sort Functions for Integer and Strings in Golang?
- Golang Program that Removes Duplicate Elements From the Array
- How to convert a slice of bytes in uppercase in Golang?
- Golang program that uses fallthrough keyword
- math.Lgamma() Function in Golang with Examples
- math.Float64bits() Function in Golang With Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.