Go built-in function copy:
Func copy (DST, src []type) int
The data that is used to slice the source (the second parameter) is copied to the target slice (the first parameter).
The return value is the number of copied data, which is the minimum value in Len (DST) and Len (SRC).
Look at the code:
Package main Import ("FMT") Func main () {varA = []int{0,1,2,3,4,5,6,7}vars = Make ([]int,6) //The source length is 8, the target is 6, and only the first 6 are copiedN1:=copy (S, a) fmt. Println ("S-", s) fmt. Println ("N1-", N1)//source length is 7, target is 6, copy index 1 to 6N2:= Copy (S, a[1:]) Fmt. Println ("S-", s) fmt. Println ("N2-", N2)//The source length is 8-5=3 and only 3 values are copied, and the latter three values in the target do not changeN3:= Copy (S, a[5:]) Fmt. Println ("S-", s) fmt. Println ("N3-", N3)//copies the index 5,6,7 from the source to the index in the destination 3,4,5N4:= Copy (s[3:], a[5:]) Fmt. Println ("S-", s) fmt. Println ("N4-", N4)}
Execution Result:
S-[0 1 2 3 4 5]
N1-6
S-[1 2 3 4 5 6]
N2-6
S-[5 6 7 4 5 6]
N3-3
S-[5 6 7 5 6 7]
N4-3
Go built-in function copy