Golang 如何把一个slice复制到另一个slice中

Golang 如何把一个slice复制到另一个slice中

Slice是一个可变长度的序列,它存储了相似类型的元素,你不允许在同一个slice中存储不同类型的元素。在Slice中,你可以使用Go语言提供的 copy()函数 将一个slice复制到另一个slice中。或者换句话说,copy()函数允许你将一个片断的元素复制到另一个片断。

语法

func copy(dst, src []Type) int

这里,dst代表目标片,src代表源片。它将返回复制的元素数量,即 len(dst)或len(src)的最小值。 让我们借助给定的例子来讨论一下。

例1 :

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

输出

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

解释: 在上面的例子中,我们有四个整数类型的片子,我们对它们进行复制操作。

  • copy_1:= copy(slc2, slc1)。 这里,slc2是目标片,slc1是源片。这里,slc2是空片,当我们试图在slc2片中复制slc1片时,复制方法将返回源片和目的片的最小长度,对于空片的slc2来说是零。
  • copy_2:= copy(slc3, slc1)。 这里,slc3是目标片断,slc1是源片断。这里,slc3片断是空片断,所以当我们试图用copy()函数将slc1片断复制到slc3时,它只从slc1片断的索引0开始复制5个元素,因为这个片断的长度是5,所以它不能存储大于5的元素。
  • copy_3:= copy(slc4, slc1)。 这里,slc4是目标片断,slc1是源片断。当我们试图用copy()函数将slc1片断复制到slc4片断时,它只从索引0开始复制4个元素。因为slc4 slice的长度是4,所以它不能存储超过4的元素。
  • copy_4:= copy(slc1, slc4)。 在这里,你可能会在其输出后感到困惑。请看,slc4已经在上面一行代码中被更新了。所以现在考虑slc4的更新值。所以现在slc4有4个元素,slc1有9个元素。所以,将被复制的元素总数为4。

例2 :

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

输出

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程