go语言数组赋值

在go语言中,可以通过一个数组给另外一个数组直接赋值(确保两个数组类型一直,即[]Type一致),但是注意到它的赋值是一个值传递

通过以下例子可以形象的看出,将数组 a(类型[3]int)赋值给变量b,可以看出 a和b的地址都不一样,且改变a里面的第一个元素a[0],并不会影响b[[0]

func main() {
	var a =[3]int{1,2,3}
	var b = a
	fmt.Println(a,b) // [1 2 3] [1 2 3]
	fmt.Printf("a address is %p\n",&a) //a address is 0xc0000160f0
	fmt.Printf("b address is %p\n",&b) //b address is 0xc000016108
	a[0] = 4
	b[0] = 5
	fmt.Println(a,b) // [4 2 3] [5 2 3]
	fmt.Printf("a address is %p\n",&a) //a address is 0xc0000160f0
	fmt.Printf("b address is %p\n",&b) //b b address is 0xc000016108
}

你可能感兴趣的:(Go语言学习,golang)