In the Go language, copy() is used to copy slices. You can copy one array slice to another array slice. If the two added array slices are not the same size, the smaller one will be used. The number of elements in that array slice is copied; the syntax is "copy(data source slice, copy destination slice)". When using the copy() function to copy a slice, if the length of the source slice is greater than the length of the destination slice, the copy will be incomplete.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
Go language's built-in function copy() can copy one array slice to another array slice. If the two added array slices are not the same size, the elements of the smaller array slice will be used. number to copy.
Go language copy(): slice copy (slice copy)
Go language slice copy uses the built-in copy() function. When using the copy() function to copy a slice, if the length of the source slice is greater than the length of the destination slice, the copy will be incomplete.
The usage format of the copy() function is as follows:
copy( destSlice, srcSlice []T) int
srcSlice is the data source slice
destSlice is the copy destination (That is, copy srcSlice to destSlice)
The target slice must have allocated space and be enough to carry the number of copied elements, and the types of the source and target must be consistent
Return value:
indicates the number of elements actually copied.
Description
Copy the slice src to the slice dst and return the number of successfully copied elements. If the length of slice src is greater than the length of dst slice, only dst slice length elements are copied.
The following code shows the process of copying one slice to another slice using the copy() function:
slice1 := []int{1, 2, 3, 4, 5} slice2 := []int{5, 4, 3} copy(slice2, slice1) // 只会复制slice1的前3个元素到slice2中 copy(slice1, slice2) // 只会复制slice2的3个元素到slice1的前3个位置
Although copying slice elements through a loop is more straightforward, The built-in copy() function is more convenient to use. The first parameter of the copy() function is the target slice to be copied, and the second parameter is the source slice. The two slices can share the same underlying array, even if there is overlap. question.
Case
1. Use code to demonstrate the impact of reference and copy operations on slice elements on slice elements.
package main import "fmt" func main() { // 设置元素数量为1000 const elementCount = 1000 // 预分配足够多的元素切片 srcData := make([]int, elementCount) // 将切片赋值 for i := 0; i < elementCount; i++ { srcData[i] = i } // 引用切片数据 refData := srcData // 预分配足够多的元素切片 copyData := make([]int, elementCount) // 将数据复制到新的切片空间中 copy(copyData, srcData) // 修改原始数据的第一个元素 srcData[0] = 999 // 打印引用切片的第一个元素 fmt.Println(refData[0]) // 打印复制切片的第一个和最后一个元素 fmt.Println(copyData[0], copyData[elementCount-1]) // 复制原始数据从4到6(不包含) copy(copyData, srcData[4:6]) for i := 0; i < 5; i++ { fmt.Printf("%d ", copyData[i]) } }
The code description is as follows:
Line 8 defines the total number of elements as 1000.
Line 11, pre-allocate an integer slice with 1000 elements, this slice will be used as the original data.
Lines 14 to 16 fill srcData with integer values from 0 to 999.
Line 19, refData refers to srcData, and the slice will not copy elements due to the equal sign operation.
Line 22, pre-allocate a slice copyData of the same size (equal size) and type as srcData.
Line 24, use the copy() function to copy the original data to the copyData slice space.
Line 27, modify the first element of the original data to 999.
On line 30, the first element of the reference data will change.
Line 33 prints the first data of the copied data. Since the data is copied, it will not change.
Line 36, copy the local data of srcData to copyData.
Lines 38 to 40 print the copyData element after copying the local data.
2. When the length of the source slice is greater than the length of the destination slice, the copy is incomplete
package main import ( "fmt" ) func main() { //当源切片的长度大于目的切片长度时,复制不完整 var sliceSrc = []string{"PHPCN", "Python", "Golang"} var sliceDst = []string{"Hello", "HaiCoder"} copy(sliceDst, sliceSrc) fmt.Println("sliceDst =", sliceDst) }
[Related recommendations:Go video tutorial、programming teaching】
The above is the detailed content of How to use copy() in Go language. For more information, please follow other related articles on the PHP Chinese website!