2
go奇怪的切片 - Go语言中文网 - Golang中文社区
source link: https://studygolang.com/articles/36281
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
go奇怪的切片
xxie · 大约10小时之前 · 181 次点击 · 预计阅读时间 1 分钟 · 大约8小时之前 开始浏览go版本go version go1.20.6 windows/amd64
func main() {
list := []int{2, 4, 8, 2}
list1 := list[0:3]
list1[0] = 10
list1 = append(list1, 12)
fmt.Println(list1, list)//打印 [10 4 8 12] [10 4 8 12]
list1 = append(list1, 5)
fmt.Println(list1, list) //打印 [10 4 8 12 5] [10 4 8 12]
}
这些还比较好理解 两个切片共用底层数组,append 超过cap长度后
接着看下面
func main() {
list := []int{2, 4, 8, 2}
list1 := list[0:3]
list1[0] = 10
fixList(list1)
fmt.Println(list1, list) //打印 [10 4 8] [10 4 8 3]
}
func fixList(param []int) {
param = append(param, 3)
}
有疑问加站长微信联系(非本文作者)
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK