代码
func main () {
copyForSlice()
}
func copyForSlice() {
xishuName := make([]string,3)
xishuName[0] = "刘备"
xishuName[1] = "关羽"
xishuName[2] = "张飞"
dongwuName := make([]string,3)
dongwuName[0] = "孙权"
dongwuName[1] = "周瑜"
copy(dongwuName,xishuName)
fmt.Println(dongwuName)
}
结果
image.png三个成员的切片 copy到两个成员的切片,目标切片变成三个成员。
反过来也是一样,他不会管目标切片是多少成员,都是保持源切片成员。
网友评论