Go中的空值


80

您如何在Go中表达“空”值?

type Node struct { 
    next *Node
    data interface{}
}

我想说

return &Node{ data: NULL, next: NULL }

Answers:


119

相当于NULLnil,因为你已经发现了。但是请注意,您通常不需要nil在Go中将事物初始化为或为零,因为默认情况下,根据类型(数字为零,引用nil),所有变量(包括动态分配的变量)都设置为“零值” 。因此,在您的示例中,这样说new(Node)将导致同时具有两个字段的Node nil




By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.