Golang Program To Copy All The Elements Of One Array To Another Array


In this tutorial, we will see to write a go language program to copy all the elements of one array to another array.

Copy All The Elements Of One Array To Another Array Using Equality Operator

Let us now look at a go language code to copy all the elements of one array to another array by using equality operator.

Algorithm

Step 1 − Impot the fmt package that.

Step 2 − Call the main() function.

Step 3 − Initialize and define an array of type strings and store values to it.

Step 4 − Print this array on the screen.

Step 5 − Create a new array named my_arr2 and use the equality operator to copy all the contents of the first array to the second.

Step 6 − The new array thus formed contain all the values of the original array. We need to print this array on the screen using fmt.Println() function.

Example

package main
import "fmt"
func main() {
   my_arr1 := [5]string{"Apple", "Mango", "Banana", "Pineapple", "Tomato"}
   my_arr2 := my_arr1
   fmt.Println("The first array, arr1 is:", my_arr1)
   fmt.Println("The array obtained after copying the contents of arr1:", my_arr2)
}

Output

The first array, arr1 is: [Apple Mango Banana Pineapple Tomato]
The array obtained after copying the contents of arr1: [Apple Mango Banana Pineapple Tomato]

Copy The Elements Of One Array To Other Using Internal Functions

Now, let us look at another example to copy the contents of one array to another. In this example we will use a predefined function called copy() to store the contents of the array.

Syntax

func copy(dst, str[] type) int

The copy function in go language is used to copy the values of one source array to the destination array and returns the number of elements copied as the result. It takes the two arrays as an argument.

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size, and capacity as arguments

Algorithm

Step 1 − First, we need to import the fmt package.

Step 2 − Then we need to call the main() function.

Step 3 − Initialize an array of integers called src and store values to it. Print this array on the screen.

Step 4 − Now, make a new array of integers called dst using the make() function.

Step 5 − Call the copy function by passing the dst and src arrays as arguments to the function and store the result as returned by this function in a new variable.

Step 6 − Print the dst array along with the number of elements that have been copied on the screen using fmt.Println() function.

Example

package main
import "fmt"
func main() {
   src := []int{1, 2, 3, 4, 5}
   fmt.Printf("The source array is: %v\n", src)
   dst := make([]int, 5)
   numberOfElementsCopied := copy(dst, src)
   fmt.Printf("The array obtained after copying the contents of src array is: %v\n", dst)
   fmt.Printf("Number Of Elements Copied: %d\n", numberOfElementsCopied)
}

Output

The source array is: [1 2 3 4 5]
The array obtained after copying the contents of src array is: [1 2 3 4 5]
Number Of Elements Copied: 5

Copy All Elements Of One Array To Another Using For Loops

Let us now write a go language program to copy the contents of one array to another using for loops.

Syntax

func len(v Type) int

The len() function is used to get the length of a variable. It takes the element as an argument and returns its length.

Algorithm

Step 1 − First, we need to import the fmt package.

Step 2 − Then we need to call the main() function.

Step 3 − Initialize an array of integers called src and store values to it. Print this array on the screen.

Step 4 − Now, make a new array of integers called dst using the make() function.

Step 5 − Now, we are using for loop to copy the contents of one array to another.

Step 6 − Print the dst array along with the number of elements that have been copied on the screen using fmt.Println() function.

Example

package main
import "fmt"
func main() {
   src := []int{1, 2, 3, 4, 5}
   fmt.Printf("The source array is: %v\n", src)
   dst := make([]int, 5)
   for i := 0; i < len(src); i++ {
      dst[i] = src[i]
   }
   fmt.Printf("The array obtained after copying the contents of src array is: %v\n", dst)
   fmt.Printf("Number Of Elements Copied: %d\n", len(dst))
}

Output

The source array is: [1 2 3 4 5]
The array obtained after copying the contents of src array is: [1 2 3 4 5]
Number Of Elements Copied: 5

Conclusion

We have successfully compiled and executed a go language program to copy all the elements of one array to another along with examples.

Updated on: 02-Jan-2023

768 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements