How do you express a "null" value in Go?
GoGo Problem Overview
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 }
Go Solutions
Solution 1 - Go
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
.
Solution 2 - Go
I just found out is nil