记一次golang slice作为函数参数调用时的坑
舒小贱 · · 1669 次点击 · · 开始浏览场景:需要删除slice中第一个元素
stackoverflow上给出解决办法:
Where a is the slice, and i is the index of the element you want to delete:
a = append(a[:i], a[i+1:]...)
... is syntax for variadic arguments in Go.
经测试有效:
s := []byte("helloworld")
s = append(s[:0], s[1:]...)
fmt.println(string(s))//输出结果为:elloworld
但是当我用函数调用的方式来删除slice中一个元素时,出现了非预期的现象:
s := []byte("helloworld")
dosomething(s)
fmt.Println(string(s))//输出异常结果:elloworldd
func dosomething(s []byte){
s = append(s[:0], s[1:]...)
//fmt.Println(string(s)) 会输出正常的 elloworld
}
发现:将slice作为参数,进行函数调用时,函数中s的值发生改变(在调用函数呃内[]byte符合预期的删除了第一个byte),但是调用方获取[]byte时,既不是预期删除了第一个byte的elloworld,也不是原始值helloworld,而是删除了第一个元素的elloworld和第最后一个元素的d的拼凑体。why??
以上预期是基于slice是引用传递,调用函数内对slice的改变,会反应给调用方,但是上面的例子表现却出乎我们意料。
改变思路,将调用函数中[]byte的改变后结果作为函数返回值返回给被调用方:
s := []byte("helloworld")
s = dosomething(s)
fmt.Println(string(s)) //输出 elloworld
func dosomething(s []byte) []byte {
s = append(s[:0], s[1:]...)
// fmt.Println(string(s)) 输出 elloworld
return s
}
作为返回值返回时,才符合我们预期。看来,slice作为函数参数引用传递时,是有坑的。
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
场景:需要删除slice中第一个元素
stackoverflow上给出解决办法:
Where a is the slice, and i is the index of the element you want to delete:
a = append(a[:i], a[i+1:]...)
... is syntax for variadic arguments in Go.
经测试有效:
s := []byte("helloworld")
s = append(s[:0], s[1:]...)
fmt.println(string(s))//输出结果为:elloworld
但是当我用函数调用的方式来删除slice中一个元素时,出现了非预期的现象:
s := []byte("helloworld")
dosomething(s)
fmt.Println(string(s))//输出异常结果:elloworldd
func dosomething(s []byte){
s = append(s[:0], s[1:]...)
//fmt.Println(string(s)) 会输出正常的 elloworld
}
发现:将slice作为参数,进行函数调用时,函数中s的值发生改变(在调用函数呃内[]byte符合预期的删除了第一个byte),但是调用方获取[]byte时,既不是预期删除了第一个byte的elloworld,也不是原始值helloworld,而是删除了第一个元素的elloworld和第最后一个元素的d的拼凑体。why??
以上预期是基于slice是引用传递,调用函数内对slice的改变,会反应给调用方,但是上面的例子表现却出乎我们意料。
改变思路,将调用函数中[]byte的改变后结果作为函数返回值返回给被调用方:
s := []byte("helloworld")
s = dosomething(s)
fmt.Println(string(s)) //输出 elloworld
func dosomething(s []byte) []byte {
s = append(s[:0], s[1:]...)
// fmt.Println(string(s)) 输出 elloworld
return s
}
作为返回值返回时,才符合我们预期。看来,slice作为函数参数引用传递时,是有坑的。