How to Copy an Array into Another Array in Golang

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.

Syntax

// creating a copy of an array by value

arr := arr1
// Creating a copy of an array by reference

arr := &arr1

Example 1: Copy an array passed by value

package main

import "fmt"

func main() {
  // Declare and initialize an array
  sourceArray := [5]int{1, 2, 3, 4, 5}

  // Declare an array with the same length as the source array
  var targetArray [5]int

  // Copy the source array into the target array
  copy(targetArray[:], sourceArray[:])

  fmt.Println("Source array:", sourceArray)
  fmt.Println("Target array:", targetArray)
}

Output

Source array: [1 2 3 4 5]

Target array: [1 2 3 4 5]

Example 2

To copy arrays with different lengths, you can use slices, but keep in mind that the copy function only copies the minimum number of elements from the source and target slices:

package main

import "fmt"

func main() {
  // Declare and initialize an array
  sourceArray := [5]int{1, 2, 3, 4, 5}

  // Declare an array with a different length than the source array
  var targetArray [3]int

  // Copy the source array into the target array
  copy(targetArray[:], sourceArray[:])

  fmt.Println("Source array:", sourceArray)
  fmt.Println("Target array:", targetArray)
}

Output

Source array: [1 2 3 4 5]

Target array: [1 2 3]

Here are three ways to copy an array into another array in Golang:

  1. Using for loop
  2. Using the copy() function
  3. Using the append() function

Method 1: Using a for loop

package main

import (
  "fmt"
)

func main() {
  originalArray := []int{1, 2, 3, 4, 5}
  copyArray := make([]int, len(originalArray))

  for i, value := range originalArray {
    copyArray[i] = value
  }

  fmt.Println("Original Array: ", originalArray)
  fmt.Println("Copy Array: ", copyArray)
}

Output

Original Array: [1 2 3 4 5]
Copy Array: [1 2 3 4 5]

Method 2: Using the copy() function

To copy an array into another array in Golang, you can use the built-in “copy()” function. However, arrays have a fixed length, and if you want to copy arrays with different sizes, consider using slices.

package main

import (
  "fmt"
)

func main() {
  originalArray := []int{11, 21, 19, 18, 10}
  copyArray := make([]int, len(originalArray))

  copy(copyArray, originalArray) 

  fmt.Println("Original Array: ", originalArray)
  fmt.Println("Copy Array: ", copyArray)
}

Output

Original Array: [11 21 19 18 10]
Copy Array: [11 21 19 18 10]

Method 3: Using the append() function

package main

import (
  "fmt"
)

func main() {
  originalArray := []int{11, 21, 19, 18, 10}
  copyArray := make([]int, 0, len(originalArray))

  copyArray = append(copyArray, originalArray...)

  fmt.Println("Original Array: ", originalArray)
  fmt.Println("Copy Array: ", copyArray)
}

Output

Original Array: [11 21 19 18 10]
Copy Array: [11 21 19 18 10]

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.