【发布时间】:2015-05-12 03:36:55
【问题描述】:
https://github.com/golang/go/blob/master/src/container/list/list.go#L49
我很难在 Go 中收到 cannot assign to pointer 错误。
这是有效的代码: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
为什么会出现此错误?
为什么 Go 在定义 next 和 prev 为指针时将 root 定义为非指针值?
谢谢
【问题讨论】:
标签: pointers go linked-list