Skip to content

Commit 6e96bb5

Browse files
committed
add
1 parent 95eecda commit 6e96bb5

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

collection/listNil.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"container/list"
5+
"fmt"
6+
)
7+
8+
//其根本原因是e是一个Element类型的指针,当然其也可能为nil,
9+
// 但是golang中list包中函数没有对其进行是否为nil的检查,变默认其非nil进行操作,所以这种情况下,便可能出现程序崩溃。
10+
11+
func main() {
12+
li := list.New()
13+
14+
li.PushBack(1)
15+
fmt.Println(li.Front().Value)//1
16+
17+
value := li.Remove(li.Front())//1
18+
fmt.Println(value)
19+
20+
value1 := li.Remove(li.Front())//panic: runtime error: invalid memory address or nil pointer dereference
21+
fmt.Println(value1)
22+
}

0 commit comments

Comments
 (0)