【golang】粗浅理解slice

一、slice的结构

type slice struct {
	array unsafe.Pointer
	len   int
	cap   int
}

从结构可以看出,slice类型的结构体含有三个元素,第一个是指向底层数组的指针,第二个是当前slice的长度,第三个是slice的容量。其中cap会以倍数方式扩容。

在《go程序设计语言》一书的第五章5.1最后一段话指出

实参是按值传递的,所以函数接收到的是每个实参的副本;修改函数的形参变量并不会影响到调用者提供的实参。然而,如提供的实参包含引用类型,比如指针、slice、map、函数或者通道,那么当函数使用形参变量时,就有可能会间接修改实参变量。

那么,slice作为形参时,是怎么影响实参变量的呢?

下面看一段代码示例以及执行结果

二、代码示例

func TestC(t *testing.T)  {
	studentNos := []int{}
	fmt.Printf("%+v, %[1]p, %d, %d,%p \n", studentNos, cap(studentNos), len(studentNos), &studentNos)
	studentNos = append(studentNos, 1)
	fmt.Printf("%+v, %[1]p, %d, %d,%p  \n", studentNos, cap(studentNos), len(studentNos), &studentNos)
	studentNos = append(studentNos, 2)
	fmt.Printf("%+v, %[1]p, %d, %d,%p  \n", studentNos, cap(studentNos), len(studentNos), &studentNos)
	studentNos = append(studentNos, 3)
	fmt.Printf("%+v, %[1]p, %d, %d,%p  \n", studentNos, cap(studentNos), len(studentNos), &studentNos)
	studentNos = append(studentNos, 4)
	fmt.Printf("%+v, %[1]p, %d, %d,%p  \n", studentNos, cap(studentNos), len(studentNos), &studentNos)
	studentNos = append(studentNos, 5)
	fmt.Printf("%+v, %[1]p, %d, %d,%p  \n", studentNos, cap(studentNos), len(studentNos), &studentNos)
	falsify(studentNos)
	fmt.Printf("%+v, %[1]p, %d, %d,%p  \n", studentNos, cap(studentNos), len(studentNos), &studentNos)
	studentNos = append(studentNos, 6)
	fmt.Printf("%+v, %[1]p, %d, %d,%p  \n", studentNos, cap(studentNos), len(studentNos), &studentNos)
}

func falsify(studentNos []int)  {
	fmt.Printf(".............%+v, %[1]p, %d, %d, %p  \n", studentNos, cap(studentNos), len(studentNos), &studentNos)
	studentNos[0]=100
	fmt.Printf(".............%+v, %[1]p, %d, %d, %p  \n", studentNos, cap(studentNos), len(studentNos), &studentNos)
	studentNos[4]=300
	fmt.Printf(".............%+v, %[1]p, %d, %d, %p  \n", studentNos, cap(studentNos), len(studentNos), &studentNos)
	studentNos = append(studentNos, 400)
	fmt.Printf(".............%+v, %[1]p, %d, %d, %p  \n", studentNos, cap(studentNos), len(studentNos), &studentNos)
	studentNos = append(studentNos, 400)
	fmt.Printf(".............%+v, %[1]p, %d, %d, %p  \n", studentNos, cap(studentNos), len(studentNos), &studentNos)
	studentNos = append(studentNos, 400)
	fmt.Printf(".............%+v, %[1]p, %d, %d, %p  \n", studentNos, cap(studentNos), len(studentNos), &studentNos)
	studentNos = append(studentNos, 400)
	fmt.Printf(".............%+v, %[1]p, %d, %d, %p  \n", studentNos, cap(studentNos), len(studentNos), &studentNos)
	studentNos = append(studentNos, 400)
	fmt.Printf(".............%+v, %[1]p, %d, %d, %p  \n", studentNos, cap(studentNos), len(studentNos), &studentNos)
	studentNos[1]=200
	fmt.Printf(".............%+v, %[1]p, %d, %d, %p  \n", studentNos, cap(studentNos), len(studentNos), &studentNos)
}

执行结果

[], 0x679188, 0, 0,0xc00005e4a0
[1], 0xc000064158, 1, 1,0xc00005e4a0
[1 2], 0xc000064180, 2, 2,0xc00005e4a0
[1 2 3], 0xc0000621a0, 4, 3,0xc00005e4a0
[1 2 3 4], 0xc0000621a0, 4, 4,0xc00005e4a0
[1 2 3 4 5], 0xc00007c0c0, 8, 5,0xc00005e4a0
.............[1 2 3 4 5], 0xc00007c0c0, 8, 5, 0xc00005e580
.............[100 2 3 4 5], 0xc00007c0c0, 8, 5, 0xc00005e580
.............[100 2 3 4 300], 0xc00007c0c0, 8, 5, 0xc00005e580
.............[100 2 3 4 300 400], 0xc00007c0c0, 8, 6, 0xc00005e580
.............[100 2 3 4 300 400 400], 0xc00007c0c0, 8, 7, 0xc00005e580
.............[100 2 3 4 300 400 400 400], 0xc00007c0c0, 8, 8, 0xc00005e580
.............[100 2 3 4 300 400 400 400 400], 0xc000098200, 16, 9, 0xc00005e580
.............[100 2 3 4 300 400 400 400 400 400], 0xc000098200, 16, 10, 0xc00005e580
.............[100 200 3 4 300 400 400 400 400 400], 0xc000098200, 16, 10, 0xc00005e580
[100 2 3 4 300], 0xc00007c0c0, 8, 5,0xc00005e4a0
[100 2 3 4 300 6], 0xc00007c0c0, 8, 6,0xc00005e4a0

结果分析:

从上面的demo我们发现

1、当slice作为函数的参数时,slice会复制一份

2、形参的slice中的指针与调用方实参的slice的指针指向同一底层数组

3、形参中len和cap与实参的len和cap完全剖离

4、当函数对实参的len范围内的元素进行修改,会影响实参的值。但对len范围外的元素修改,对实参无影响。

5、注意,当在函数中,slice发生扩容,其指针将指向新的地址,与实参中指针指向不同的底层数组。此后再对形参的slice中的元素做任何操作,都不会影响实参。

 

总结:slice作为函数参数使用时需谨慎

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值