Copy an Array by Value and Reference into Another Array in Golang
Array in Golang is a numbered sequence of elements of the same type. The size of the array is fixed. We can access the elements by their index. You can declare an array of size n and type T specifying this way mentioned below.
Go doesn’t have an inbuilt function to copy an array to another array. There are two ways of copying an array to another array:
- By Value
- By Reference
1. Copying By Value: If we copy an array by value and later changes are made in values of original array, the same changes won’t be reflected in the copy of the original array.
2. Copying By Reference: If we copy an array by reference and later make any changes in the original array, then they will reflect in the copy of the original array created.
strArray: [Apple Mango Guava] Arraybyval : [Apple Mango Guava] *Arraybyref : [Apple Mango Guava] After making changesstrArray: [Watermelon Mango Guava] Arraybyval: [Apple Mango Guava] *Arraybyref: [Watermelon Mango Guava]