Go Slice Copy

In Go, slices are references to an underlying array, meaning modifications to one slice can affect other slices referencing the same data. To create a separate copy of a slice with its own underlying array, you can use the built-in copy() function.

In this tutorial, we will explore how to use the copy() function to duplicate slices, examine its behavior with different lengths, and provide practical examples with detailed explanations.


Steps to Copy a Slice

  • Create the Destination Slice: Allocate memory for the destination slice with sufficient length.
  • Use the copy() Function: Call copy(dest, src) to copy elements from the source slice to the destination slice.
  • Handle Length Mismatch: If the destination slice is shorter than the source, only the overlapping elements will be copied.

Examples of Slice Copy

1. Copy a Slice with Equal Length

This example demonstrates how to copy a slice when the source and destination slices have equal lengths:

</>
Copy
package main

import "fmt"

func main() {
    // Source slice
    source := []int{10, 20, 30, 40, 50}

    // Destination slice with the same length
    destination := make([]int, len(source))

    // Copy the source slice to the destination slice
    copy(destination, source)

    // Print the source and destination slices
    fmt.Println("Source Slice:", source)
    fmt.Println("Destination Slice:", destination)
}

Explanation

  1. Create Source Slice: The source slice is initialized with five integers.
  2. Create Destination Slice: The destination slice is created with the same length as the source slice using make([]int, len(source)).
  3. Copy Elements: The copy(destination, source) function copies all elements from source to destination.
  4. Print Results: Both slices are printed to confirm the successful copy.

Output


2. Copy a Slice with Shorter Destination

This example demonstrates how to copy a slice when the destination slice is shorter than the source slice:

</>
Copy
package main

import "fmt"

func main() {
    // Source slice
    source := []int{10, 20, 30, 40, 50}

    // Destination slice with a shorter length
    destination := make([]int, 3)

    // Copy the source slice to the destination slice
    copy(destination, source)

    // Print the source and destination slices
    fmt.Println("Source Slice:", source)
    fmt.Println("Destination Slice:", destination)
}

Explanation

  1. Create Source Slice: The source slice is initialized with five integers.
  2. Create Destination Slice: The destination slice is created with a shorter length of 3 using make([]int, 3).
  3. Copy Overlapping Elements: The copy(destination, source) function copies only the first three elements from source to destination, as the destination slice cannot accommodate more.
  4. Print Results: The slices are printed to show that only the overlapping elements were copied.

Output


3. Copy a Slice to a Larger Destination

This example demonstrates how to copy a slice when the destination slice is larger than the source slice:

</>
Copy
package main

import "fmt"

func main() {
    // Source slice
    source := []int{10, 20, 30}

    // Destination slice with a larger length
    destination := make([]int, 5)

    // Copy the source slice to the destination slice
    copy(destination, source)

    // Print the source and destination slices
    fmt.Println("Source Slice:", source)
    fmt.Println("Destination Slice:", destination)
}

Explanation

  1. Create Source Slice: The source slice is initialized with three integers.
  2. Create Destination Slice: The destination slice is created with a larger length of 5 using make([]int, 5).
  3. Copy Elements: The copy(destination, source) function copies all elements from source to the first three positions in destination, leaving the remaining positions as their default value (0).
  4. Print Results: The slices are printed to show that the source slice elements were copied and additional space remains in the destination slice.

Output


Points to Remember

  • Efficient Copying: The copy() function is optimized for copying slices efficiently.
  • Length Mismatch: When the destination slice is shorter than the source, only the overlapping elements are copied.
  • Mutual Independence: After copying, the destination slice is independent of the source slice.
  • Default Values: If the destination slice is longer than the source slice, extra positions are initialized with default values of the element type.