简体   繁体   English

从非指针结构元素转到链接列表指针分配

[英]Go linked list pointer assignment from non-pointer struct element

https://github.com/golang/go/blob/master/src/container/list/list.go#L49 https://github.com/golang/go/blob/master/src/container/list/list.go#L49

I am having hard time why I am getting cannot assign to pointer error in Go. 我很难理解为什么我cannot assign to pointer在Go中cannot assign to pointer错误。

Here's the code that works: http://play.golang.org/p/P9FjK8A-32 which is same as Go's original container/list code 这是有效的代码: 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{}
}

The original code has root as a value and reference it everytime it needs to be in pointer type but why not at first place define root as a pointer? 原始代码将root作为值,并在每次需要使用指针类型时都引用它,但为什么不首先将root定义为指针呢?

type List struct {
    root *Element
    len  int
}

type Element struct {
    next, prev *Element
    list       *List
    Value      interface{}
}

This give me an error: http://play.golang.org/p/1gCAR_rcx1 -> invalid memory address or nil pointer dereference 这给我一个错误: http : //play.golang.org/p/1gCAR_rcx1- > invalid memory address or nil pointer dereference

Why am I getting this error? 为什么会出现此错误? Why does Go define root as a non-pointer value when it defines next , and prev as pointers? 为什么Go在将nextprev定义为指针时将root定义为非指针值?

Thanks 谢谢

A pointer is nil by default and needs to be initialized. 指针默认为nil ,需要初始化。

This: 这个:

// Init initializes or clears list l.
func (l *List) Init() *List {
  l.root.next = l.root
  l.root.prev = l.root
  l.len = 0
  return l
}

should become this: 应该变成这个:

// Init initializes or clears list l.
func (l *List) Init() *List {
  l.root = new(Element) // necessary to avoid dereferencing a nil pointer
  l.root.next = l.root
  l.root.prev = l.root
  l.len = 0
  return l
}

Demo at http://play.golang.org/p/EYSscTMYnn 演示在http://play.golang.org/p/EYSscTMYnn

In the case of the standard library, it is not necessary to have root be a pointer, however, for prev and next it is necessary, otherwise the struct definition would be recursive, which is not allowed, because it would in theory cause a struct of infinite size... 对于标准库,没有必要让root成为指针,但是,对于prevnext则是必须的,否则struct定义将是递归的,这是不允许的,因为从理论上讲它将导致struct无限大小...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:从链表结构中不兼容的指针类型分配 - Warning: assignment from incompatible pointer type in linked list struct 从链表中不兼容的指针类型分配? - assignment from incompatible pointer type in linked list? 编译器错误,链接列表:错误:“->”的基操作数具有非指针类型“IntNodeType” - Compiler Error, Linked list: error: base operand of ‘->’ has non-pointer type ‘IntNodeType’ Go中的非指针错误,不确定是什么意思 - non-pointer error in Go not sure what it means 如何从 Go 列表中检索结构指针 - How to retrieve struct pointer from Go list C ++,将非指针类型赋值给模板类的成员指针 - C++, assignment of non-pointer type to a member pointer of template class 使用指向结构向量的指针访问结构成员。 错误:“->”的基本操作数具有非指针类型 - Acessing a struct member, using a pointer to a vector of structs. Error:base operand of '->' has non-pointer type 如何使用指针算法指向链表结构的下一个元素? - how to point to next element of a linked list struct using pointer arithmetic? 为什么带指针和非指针接收器的方法在Go中不能同名? - Why can't methods with pointer and non-pointer receivers have the same name in Go? 从不兼容的指针类型(结构,链表)分配 - Assignment from incompatible pointer type (structs, linked list)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM