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 a map to another map in Golang?
- io.Copy() Function in Golang with Examples
- Copy the Sign of Given Number in Golang
- reflect.Copy() Function in Golang with Examples
- How to Copy Struct Type Using Value and Pointer Reference in Golang?
- How to pass an Array to a Function in Golang?
- How to iterate over an Array using for loop in Golang?
- Program to shift all zero to the end of array in Golang
- Golang Pointer to an Array as Function Argument
- How to Convert a zero terminated byte array to string in Golang?
- Golang Program to Find Largest Element in an Array
- Golang Program that Removes Duplicate Elements From the Array
- How to use Array Reverse Sort Functions for Integer and Strings in Golang?
- How to copy one slice into another slice in Golang?
- Zero value in Golang
- Using WaitGroup in Golang
- Recover in Golang
- Templates in GoLang
- Inheritance in GoLang
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.