Below you will find pages that utilize the taxonomy term “slice”
May 3, 2020
Golang遍历切片删除元素引起恐慌问题
"删除一个切片的部分元素, 告知切片操作:Golang遍历切片恐慌时删除元素\n问题描述 代码( 演示代码):\npackage main import ( \u0026#34;fmt\u0026#34; ) func main() { slice := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} for i, value := range slice { if value%3 == 0 { // remove 3, 6, 9 slice = append(slice[:i], slice[i+1:]...) } } fmt.Printf(\u0026#34;%v\u0026#34;, slice) } 运行结果\npanic: runtime error: slice bounds out of range [8:6] goroutine 1 [running]: main.main() /tmp/sandbox2635969259/prog.go:11 +0x212 Program exited. 解决办法: 以下是网友想到的几种办法"