英文:
How do you express a "null" value in Go?
问题
在Go语言中,如何表示一个"null"值?
type Node struct {
next *Node
data interface{}
}
我想要表达的是
return &Node{ data: nil, next: nil }
英文:
How do you express a "null" value in Go?
type Node struct {
next *Node
data interface{}
}
And I want to say
return &Node{ data: NULL, next: NULL }
答案1
得分: 141
NULL
的等价物是nil
,正如你已经发现的那样。但是请注意,通常在Go语言中不需要将变量初始化为nil
或零,因为默认情况下,所有变量(包括动态分配的变量)都会根据类型设置为“零值”(数字为零,引用为nil
)。因此,在你的例子中,使用new(Node)
会得到一个两个字段都为nil
的Node。
英文:
The equivalent of NULL
is nil
, as you already discovered. Note, though, that you don't generally need to initialize things to nil
or zero in Go, because by default all variables (including dynamically allocated ones) are set to “zero values” according to type (numbers zero, references nil
). So in your example saying new(Node)
would result in a Node with both fields nil
.
答案2
得分: 25
我刚刚发现是nil
英文:
I just found out is nil
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论