Go Program to Copy a Slice

Introduction

In Go, you can copy the elements of one slice to another using the built-in copy function. This function copies elements from a source slice to a destination slice. The number of elements copied is the minimum of the lengths of the source and destination slices. This guide will demonstrate how to copy a slice in Go.

Problem Statement

Create a Go program that:

  • Declares and initializes a source slice.
  • Declares a destination slice.
  • Copies the elements from the source slice to the destination slice.
  • Displays both the source and destination slices.

Example:

  • Input: Source slice: [1, 2, 3, 4, 5]
  • Output:
    • Source slice: [1, 2, 3, 4, 5]
    • Destination slice: [1, 2, 3, 4, 5]

Solution Steps

  1. Import the fmt Package: Use import "fmt" for formatted I/O operations.
  2. Write the Main Function: Define the main function, which is the entry point of every Go program.
  3. Create and Initialize the Source Slice: Use a slice literal to create and initialize the source slice.
  4. Create the Destination Slice: Use the make function to create a slice with the same length as the source slice.
  5. Copy the Elements: Use the copy function to copy elements from the source slice to the destination slice.
  6. Display the Slices: Use fmt.Println to display the source and destination slices.

Go Program

package main

import "fmt"

/**
 * Go Program to Copy a Slice
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 1: Create and initialize the source slice
    sourceSlice := []int{1, 2, 3, 4, 5}
    fmt.Println("Source slice:", sourceSlice)

    // Step 2: Create the destination slice with the same length as the source slice
    destinationSlice := make([]int, len(sourceSlice))

    // Step 3: Copy the elements from the source slice to the destination slice
    copy(destinationSlice, sourceSlice)

    // Step 4: Display the destination slice
    fmt.Println("Destination slice:", destinationSlice)
}

Explanation

Step 1: Create and Initialize the Source Slice

  • The source slice is created and initialized using a slice literal. For example, sourceSlice := []int{1, 2, 3, 4, 5} creates a slice of integers with five elements.

Step 2: Create the Destination Slice

  • The destination slice is created using the make function. It is initialized with the same length as the source slice to ensure that all elements can be copied. For example, destinationSlice := make([]int, len(sourceSlice)) creates a slice of integers with the same length as the source slice.

Step 3: Copy the Elements

  • The copy function is used to copy the elements from the source slice to the destination slice. For example, copy(destinationSlice, sourceSlice) copies all the elements from sourceSlice to destinationSlice.

Step 4: Display the Slices

  • The program prints the source and destination slices using fmt.Println to display their elements.

Output Example

Example 1:

Source slice: [1 2 3 4 5]
Destination slice: [1 2 3 4 5]

Example 2:

If the destination slice has a smaller length than the source slice, only the number of elements that fit in the destination slice will be copied:

destinationSlice := make([]int, 3)  // Smaller length than source slice
copy(destinationSlice, sourceSlice)
fmt.Println("Destination slice:", destinationSlice)

Output:

Destination slice: [1 2 3]

Conclusion

This Go program demonstrates how to copy a slice using the copy function. It covers basic programming concepts such as slice creation, copying elements, and working with slices of different lengths. This example is useful for beginners learning Go programming and understanding how to duplicate and manage slices.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top