English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Go Language Slice CopyslicesIs similar type storage elements, it is not allowed to store different types of elements in the same variable length sequence. In slices, you can use Go language providedcopy() function
Copy one slice to another slice. In other words, you can copy the elements of one slice to another slice through the copy() function.
Syntax:
func copy(dst, src []Type) intHere,Represents the target slice, andsrcRepresents the source slice. It will return the number of elements to be copied, which shouldTolen(dst)Orlen(src)OfMinimum valueLet's use the given example to demonstrate how to use it:
Example package main import "fmt" //Create a slice elements. Since1 := []int{58, 69, 40, 45, 11, 56, 67, 21, 65} var slc2 []int elements. Since3 := make([]int, 5) elements. Since4 := []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) //Copy slice elements.1 := copy(slc2, slc1) fmt.Println("\nSlice:", slc2) fmt.Println("Total number of copied elements:", copy_1) elements.2 := copy(slc3, slc1) fmt.Println("\nSlice:", slc3) fmt.Println("Total number of copied elements:", copy_2) elements.3 := copy(slc4, slc1) fmt.Println("\nSlice:", slc4) fmt.Println("Total number of copied elements:", copy_3) //Do not confuse here, because in the above //Copied slc4Code line //And therefore make permanent changes, that is // elements. Since 4Contains[58 69 40 45] elements.4 := copy(slc1, slc4) fmt.Println("\nSlice:", slc1) fmt.Println("Total number of copied elements:", 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 copied elements: 0 Slice: [58 69 40 45 11] Total number of copied elements: 5 Slice: [58 69 40 45] Total number of copied elements: 4 Slice: [58 69 40 45 11 56 67 21 65] Total number of copied elements: 4
Usage instructions:In the above example, we have four integer type slices and perform copy operations on them:
copy 1:= copy(slc2, slc1):在这里,slc2, slc1): Here, slc2is the target slice, slc1is the source slice. Here, when we try to copy in slc2slice copies slc2slice, slc
elements.2copy_3:= copy(slc1is nil slice, then the copy method will return the minimum length of the source slice and the target slice, for the empty slice slc3, the minimum length is 0.1): Here, slc is the target slice, and slc3is the source slice.1here, slc3slice is an empty slice, so when we try to use the copy() function to5slice to slc1when, since the length of the slice is5, so it only copies from slc5slice has copied elements, because the length of the slice is5, so it cannot store more than
elements.3copy_4:= copy(slc1, so it can): It does not store more thanelements. Since4here,elements. Since1is the target slice, andelements. Since1 is the source slice. When we try to use the copy() function toelements. Since4 slice to4slice starts copying from index 0, and it copieselements. Since4 slc4The length of the slice is4, so it cannot store more than
elements.4copy_1:= copy(slc4, slc4): You may feel confused after the output. See, the slc4has been updated. Therefore, now consider slc4elements, while slc4has been updated. Therefore, now slc1elements, while slc9with4The total number of elements to be copied is
.2Example
Example package main import "fmt" //func main() { Create the target slice, using the make function1 Source slice3:= []string{"w3codeboxs ", "for", "w //codeboxs ", "GFG"} Create the target slice, using the make function2 slice_ 3) //:= make([]string, fmt.Println("Slice_1: ", slice_1) fmt.Println("Slice_2: ", slice_2) //Before copying slice_1copy to slice_2 Copy_1 := copy(slice_2, slice_1) fmt.Println("\nSlice_1: ", slice_1) fmt.Println("Slice_2: ", slice_2) fmt.Println("Number of copied elements: ", Copy_1) //Copy slice //Use copy feature //Clear code Copy_2 := copy(slice_1, []string{"123w3codeboxs ", "gfg"}) fmt.Println("\nSlice_1 : ", slice_1) fmt.Println("Number of copied elements:", Copy_2) }
Output:
Slice_1: [3codeboxs for w3codeboxs GFG] Slice_2: [ ] Slice_1: [3codeboxs for w3codeboxs GFG] Slice_2: [3codeboxs for w3codeboxs] Number of Copied Elements: 3 Slice_1: [123w3codeboxs gfg w3codeboxs GFG] Number of Copied Elements: 2