Open In App

How to copy one slice into another slice in Golang?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

A Slice is a variable-length sequence that stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In Slice, you can copy one slice into another slice using the copy() function provided by the Go language. Or in other words, copy() function allows you to copy the elements of one slice into another slice. 

Syntax:

func copy(dst, src []Type) int

Here, dst represents the destination slice and src represents the source slice. It will return the number of elements copied that will be the minimum of len(dst) or len(src). Let us discuss with the help of the given example:

Example 1: 

Go




// Go program to illustrate how to copy
// a slice into another slice using the
// copy function
package main
 
    import "fmt"
 
    // Main Method
    func
    main()
{
 
// Creating slices
slc1:
    =
        [] int {
            58, 69, 40, 45, 11, 56, 67, 21, 65
        } var slc2[] int slc3 : = make([] int, 5) slc4
        : = [] int { 78, 50, 67, 77 }
 
            // Before copying
            fmt.Println("Slice_1:", slc1)
                fmt.Println("Slice_2:", slc2)
                    fmt.Println("Slice_3:", slc3)
                        fmt.Println("Slice_4:", slc4)
 
            // Copying the slices
            copy_1
        : = copy(slc2, slc1)
                fmt.Println("\nSlice:", slc2) fmt.Println(
                    "Total number of elements copied:",
                    copy_1)
 
                    copy_2
        : = copy(slc3, slc1)
                fmt.Println("\nSlice:", slc3) fmt.Println(
                    "Total number of elements copied:",
                    copy_2)
 
                    copy_3
        : = copy(slc4, slc1)
                fmt.Println("\nSlice:", slc4) fmt.Println(
                    "Total number of elements copied:",
                    copy_3)
 
            // Don't confuse here, because in above
            // line of code the slc4 has been copied
            // and hence modified permanently i.e.
            // slc 4 contains [58 69 40 45]
            copy_4
        : = copy(slc1, slc4)
                fmt.Println("\nSlice:", slc1) fmt.Println(
                    "Total number of elements copied:",
                    copy_4)
}


Output:

Slice_1: [58 69 40 45 11 56 67 21 65]
Slice_2: []
Slice_3: [0 0 0 0 0]
Slice_4: [78 50 67 77]

Slice: []
Total number of elements copied: 0

Slice: [58 69 40 45 11]
Total number of elements copied: 5

Slice: [58 69 40 45]
Total number of elements copied: 4

Slice: [58 69 40 45 11 56 67 21 65]
Total number of elements copied: 4

Explanation: In the above example we have four integer type slices and we perform copy operation on them:

  • copy_1:= copy(slc2, slc1): Here, slc2 is the destination slice and slc1 is the source slice. Here, slc2 is the nil slice when we try to copy slc1 slice in slc2 slice, then copy method will return the minimum of length of source and destination slice which is zero for empty slice slc2.
  • copy_2:= copy(slc3, slc1): Here, slc3 is the destination slice and slc1 is the source slice. Here, the slc3 slice is the empty slice, so when we try to copy slc1 slice into slc3 using copy() function it copies only 5 elements from the slc1 slice starting from index 0 because the length of this slice is 5, so it can not store elements greater than 5.
  • copy_3:= copy(slc4, slc1): Here, slc4 is the destination slice and slc1 is the source slice. When we try to copy slc1 slice into slc4 slice using copy() function it copies only 4 elements in it starting from index 0. Because the length of the slc4 slice is 4, so it can not store elements more than 4.
  • copy_4:= copy(slc1, slc4): Here, you might be confused after its output. See, slc4 has been updated in the above line of code. So now consider the updated values for slc4. So now slc4 has 4 elements and slc1 has 9 elements. So, the total number of elements that will be copied is 4.

Example 2: 

Go




// Go program to illustrate how to copy
// a slice into another slice using the
// copy function
package main
 
    import "fmt"
 
    func
    main()
{
 
// source slice
slice_1:
    = [] string { "Geeks", "for", "Geeks", "GFG" }
 
    // creating destination slice
    // using make function
    slice_2 : = make([] string, 3)
 
                // Before Copying
                fmt.Println("Slice_1: ", slice_1)
                    fmt.Println("Slice_2: ", slice_2)
 
                // Copying slice_1 into slice_2
                // using copy function
                Copy_1
        : = copy(slice_2, slice_1)
                fmt.Println("\nSlice_1: ", slice_1)
                    fmt.Println("Slice_2: ", slice_2)
                        fmt.Println(
                            "Number of elements copied: ",
                            Copy_1)
 
            // Copying the slice
            // using copy function
            // see the code clearly
            Copy_2
        : = copy(slice_1, [] string { "123geeks", "gfg" })
                fmt.Println("\nSlice_1 : ", slice_1)
                    fmt.Println(
                        "Number of elements copied:",
                        Copy_2)
}


Output:

Slice_1:  [Geeks for Geeks GFG]
Slice_2:  [  ]

Slice_1:  [Geeks for Geeks GFG]
Slice_2:  [Geeks for Geeks]
Number of elements copied:  3

Slice_1:  [123geeks gfg Geeks GFG]
Number of elements copied: 2

To copy one slice into another slice in Go, you can use the built-in function copy. The copy function takes two arguments: the destination slice and the source slice, and it copies the elements from the source slice into the destination slice.

Here’s an example that demonstrates how to copy one slice into another slice in Go:

Go




package main
 
import "fmt"
 
func main() {
    source := []int{1, 2, 3, 4, 5}
    destination := make([]int, len(source))
    copy(destination, source)
 
    fmt.Println("Source: ", source)
    fmt.Println("Destination: ", destination)
}


Output:

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

In this example, the slice source is created with the elements 1, 2, 3, 4, 5. The slice destination is created with the same length as the slice source by calling the function make, and it’s initialized to 0. The function copy is then called with destination and source as arguments to copy the elements from source into destination.

It’s important to note that the length of the destination slice must be equal to or greater than the length of the source slice, otherwise the copy function will panic. If the destination slice is longer than the source slice, the remaining elements in the destination slice will be unchanged.

Slice copying is a common operation in Go, and the copy function provides a simple and efficient way to copy slices. It’s widely used in Go programs to make copies of slices for various purposes.



Last Updated : 02 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads