https://github.com/golang/go/blob/master/src/container/list/list.go#L49我很難理解為什么我cannot assign to pointer在 Go 中出錯。這是有效的代碼:http : //play.golang.org/p/P9FjK8A-32與 Go 的原始容器/列表代碼相同type List struct { root Element len int}type Element struct { next, prev *Element list *List Value interface{}}原始代碼具有root作為值并在每次需要采用指針類型時引用它,但為什么不首先定義root為指針?type List struct { root *Element len int}type Element struct { next, prev *Element list *List Value interface{}}這給了我一個錯誤:http : //play.golang.org/p/1gCAR_rcx1 ->invalid memory address or nil pointer dereference為什么我收到這個錯誤?為什么去定義root為非指針的值時,它定義next,并prev作為指針?
- 1 回答
- 0 關注
- 218 瀏覽
添加回答
舉報
0/150
提交
取消
