HiveBrain v1.2.0
Get Started
← Back to all entries
snippetgoCritical

How do you express a "null" value in Go?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
youhowvalueexpressnull

Problem

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 }

Solution

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.

Context

Stack Overflow Q#4217864, score: 146

Revisions (0)

No revisions yet.