一、语法
go 语言的内置函数 copy() 能够将一个数组切片复制到另一个数组切片中,如果退出的两个数组切片不一样大,就会依照其中较小的那个数组切片的元素个数进行复制。
func copy(dst, src []Type) int
应用 copy 有几个特色
- 如果之前是两个不同的地址,copy 后也是两个不同的地址,扭转其中一个不影响另一个。
- copy 到指标 slice,到底复制多少,取决于它的大小以及起始地位。
- 更像是一种指定开始索引的替换
二、应用示范
1、同样的大小
func TestCopy1(t *testing.T) {from := []int{1, 2, 3}
var to []int = make([]int, len(from), cap(from))
t.Logf("from slice: %[1]v, address: %[1]p\n", from)
t.Logf("to slice: %[1]v, address: %[1]p\n", to)
copy(to, from)
t.Log("copy(to, from) 后:")
t.Logf("from slice: %[1]v, address: %[1]p\n", from)
t.Logf("to slice: %[1]v, address: %[1]p\n", to)
to[0] = 11
t.Log("to[0] = 11 后:")
t.Logf("from slice: %[1]v, address: %[1]p\n", from)
t.Logf("to slice: %[1]v, address: %[1]p\n", to)
}
输入:
from slice: [1 2 3], address: 0xc0000cc060
to slice: [0 0 0], address: 0xc0000cc080
copy(to[2:], from) 后:from slice: [1 2 3], address: 0xc0000cc060
to slice: [1 2 3], address: 0xc0000cc080
to[0] = 11 后:from slice: [1 2 3], address: 0xc0000cc060
to slice: [11 2 3], address: 0xc0000cc080
2、指标的 slice 为长度 len 为 0 的时候,复制不胜利,还是为空 []
func TestCopy2(t *testing.T) {from := []int{1, 2, 3}
var to []int = make([]int, 0, cap(from))
t.Logf("from slice: %[1]v, address: %[1]p\n", from)
t.Logf("to slice: %[1]v, address: %[1]p\n", to)
copy(to, from)
t.Log("copy(to, from) 后:")
t.Logf("from slice: %[1]v, address: %[1]p\n", from)
t.Logf("to slice: %[1]v, address: %[1]p\n", to)
}
输入:
from slice: [1 2 3], address: 0xc000144060
to slice: [], address: 0xc000144080
copy(to, from) 后:from slice: [1 2 3], address: 0xc000144060
to slice: [], address: 0xc000144080
3、指标的 slice 为长度 len 为 1 的时候,只能复制第一个
func TestCopy3(t *testing.T) {from := []int{1, 2, 3}
var to []int = make([]int, 1, cap(from))
t.Logf("from slice: %[1]v, address: %[1]p\n", from)
t.Logf("to slice: %[1]v, address: %[1]p\n", to)
copy(to, from)
t.Log("copy(to, from) 后:")
t.Logf("from slice: %[1]v, address: %[1]p\n", from)
t.Logf("to slice: %[1]v, address: %[1]p\n", to)
}
输入:
from slice: [1 2 3], address: 0xc0000cc060
to slice: [0], address: 0xc0000cc080
copy(to, from) 后:from slice: [1 2 3], address: 0xc0000cc060
to slice: [1], address: 0xc0000cc080
4、指标的 slice 为长度 len 远大于的时候,替换开始的一部分
func TestCopy4(t *testing.T) {from := []int{1, 2, 3}
var to []int = make([]int, 10, 10)
t.Logf("from slice: %[1]v, address: %[1]p\n", from)
t.Logf("to slice: %[1]v, address: %[1]p\n", to)
copy(to, from)
t.Log("copy(to, from) 后:")
t.Logf("from slice: %[1]v, address: %[1]p\n", from)
t.Logf("to slice: %[1]v, address: %[1]p\n", to)
}
输入:
from slice: [1 2 3], address: 0xc000016160
to slice: [0 0 0 0 0 0 0 0 0 0], address: 0xc0000200f0
copy(to, from) 后:from slice: [1 2 3], address: 0xc000016160
to slice: [1 2 3 0 0 0 0 0 0 0], address: 0xc0000200f0
5、指定开始的替换索引 copy(to[3:], from)
func TestCopy5(t *testing.T) {from := []int{1, 2, 3}
var to []int = make([]int, 10, 10)
t.Logf("from slice: %[1]v, address: %[1]p\n", from)
t.Logf("to slice: %[1]v, address: %[1]p\n", to)
copy(to[3:], from)
t.Log("copy(to[3:], from) 后:")
t.Logf("from slice: %[1]v, address: %[1]p\n", from)
输入:
from slice: [1 2 3], address: 0xc000016160
to slice: [0 0 0 0 0 0 0 0 0 0], address: 0xc0000200f0
copy(to[3:], from) 后:from slice: [1 2 3], address: 0xc000016160
to slice: [0 0 0 1 2 3 0 0 0 0], address: 0xc0000200f0