How to copy one slice into another slice in Golang?

A Slice is a variable-length sequence which 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
      
}
chevron_right

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:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
  
}
chevron_right

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




Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :