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 }
There are two packages: null and its subpackage zero . Types in null will only be considered null on null input, and will JSON encode to null . If you need zero and null be considered separate values, use these. Types in zero are treated like zero values in Go: blank string input will produce a null zero.
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 ).
nil is a frequently used and important predeclared identifier in Go. It is the literal representation of zero values of many kinds of types. Many new Go programmers with experiences of some other popular languages may view nil as the counterpart of null (or NULL ) in other languages.
In the Go programming language, nil is a zero value. Recall from unit 2 that an integer declared without a value will default to 0. An empty string is the zero value for strings, and so on. A pointer with nowhere to point has the value nil .
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
.
I just found out is nil
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With