Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null value in Go

Tags:

go

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 } 
like image 342
fabrizioM Avatar asked Nov 18 '10 17:11

fabrizioM


People also ask

Is there null in Go?

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.

How do you define null in Golang?

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 ).

Is nil null in Go?

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.

Why does Go have nil?

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 .


2 Answers

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.

like image 138
Arkku Avatar answered Sep 22 '22 02:09

Arkku


I just found out is nil

like image 20
fabrizioM Avatar answered Sep 19 '22 02:09

fabrizioM