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]
- How to pass an Array to a Function in Golang?
- Golang Pointer to an Array as Function Argument
- How to copy one slice into another slice in Golang?
- Zero value in Golang
- Strings in Golang
- Structures in Golang
- Recover in Golang
- Channel in Golang
- Panic in Golang
- Methods in Golang
- Pointers in Golang
- Golang Maps
- Methods With Same Name in Golang
- Hello World in Golang
- Interfaces 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.