golang slice 遍历删除
package main
import "fmt"
/*slice 遍历删除示例*/
func main() {
//定义一个数字切片
ageList := []int{1, 3, 7, 7, 8, 2, 5}
//遍历删除6以下的
for i := 0; i < len(ageList); {
if ageList[i] < 6 {
ageList = append(ageList[:i], ageList[i+1:]...)
} else {
i++
}
}
fmt.Printf("after del:%v", ageList)
}
输出 after del:[7 7 8]
func main() {
// create a zero-length slice with the same underlying array
tmp := people[:0]
for _, p := range people {
if !p.Remove {
tmp = append(tmp, p)
}
}
fmt.Println(tmp) // [{P0 false} {P2 false}]
}