for循环删除的时候要用临时变量保存e.Next(),删除后再赋值给e,否则删除后e=nil就直接退出了
func TestList(t *testing.T) { l := list.New() for i := 0; i < 10; i++ { l.PushBack(i) } //fmt.Printf("%v",l.) for e := l.Front(); e != nil; e = e.Next() { fmt.Print(e.Value.(int)) } fmt.Println("") var next *list.Element for e := l.Front(); e != nil; { if e.Value.(int) == 5 || e.Value.(int) == 7 { next = e.Next() l.Remove(e) e = next } else { e = e.Next() } } for e := l.Front(); e != nil; e = e.Next() { fmt.Print(e.Value.(int)) } t.Errorf("%v",l) }