go数组和slice

数组是值拷贝,slice是指针拷贝

func main() {
a1 := []int{1, 2, 3, 4, 5, 6}
a2 := a1

a2 = a1[0:2] //slice是指针拷贝,slice底层实现还是array
a2[0]  = 111

x := [3]int{1,2,3} //数组是值拷贝
y := x
y[0] = 2
fmt.Println("a1=", a1, "a2=", a2)
fmt.Println("x=", x, "y=", y)

}
a1= [111 2 3 4 5 6] a2= [111 2]
x= [1 2 3] y= [2 2 3]

你可能感兴趣的:(go数组和slice)