我的目的是从特定切片中删除一个元素,代码如下:
func main() {
s := []int{0, 1, 2, 3, 4}
remove(s, 3)
fmt.Println(s, len(s), cap(s))
}
func remove(s []int, idx int) {
if idx < 0 || idx >= len(s) {
return
}
copy(s[idx:], s[idx+1:])
s = s[:len(s)-1]
fmt.Println(s, len(s), cap(s))
}
Run Code Online (Sandbox Code Playgroud)
但输出显示:
[0 1 2 4] 4 5
[0 1 2 4 4] 5 5
据我所知,slice将作为引用类型传递给函数调用,为什么它不能修改它?