Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does nil mean in golang?

Tags:

go

There are many cases using nil in golang. For example:

func (u *URL) Parse(ref string) (*URL, error) {
    refurl, err := Parse(ref)
    if err != nil {
        return nil, err
    }
    return u.ResolveReference(refurl), nil
}

but we can't use it like this:

var str string //or var str int
str = nil

the golang compiler will throw a can't use nil as type string in assignment error.

Looks like nil can only be used for a pointer of struct and interface. If that is the case, then what does it mean? and when we use it to compare to the other object, how do they compare, in other words, how does golang determine one object is nil?

EDIT:For example, if an interface is nil, its type and value must be nil at the same time. How does golang do this?

like image 657
starkshang Avatar asked Mar 14 '16 08:03

starkshang


People also ask

Why does Golang use nil instead of null?

nil s 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.

What is a nil variable?

Nil is a type with a single value, nil, whose main property is to be different from any other value. As we have seen, a global variable has a nil value by default, before a first assignment, and you can assign nil to a global variable to delete it.

Is nil or empty Golang?

In Golang, when a variable is declared without an initialization value, its value will be set to the type's zero value. The zero value of a Slice is nil, so in our example above, when we declare var foo []string the value of foo is actually nil not an empty slice of string [] .

Is nil false in Golang?

The last example shows that nil is not a type therefore it cannot be used to be assigned to a value without a type. What happened here? We can clearly see (and based on previous rules) that both p and i has value nil but the condition is evaluated into false. This is not a bug it is a legitimate Golang behaviour.


4 Answers

In Go, nil is the zero value for pointers, interfaces, maps, slices, channels and function types, representing an uninitialized value.

nil doesn't mean some "undefined" state, it's a proper value in itself. An object in Go is nil simply if and only if it's value is nil, which it can only be if it's of one of the aforementioned types.

An error is an interface, so nil is a valid value for one, unlike for a string. For obvious reasons a nil error represents no error.

like image 170
LemurFromTheId Avatar answered Oct 17 '22 11:10

LemurFromTheId


nil in Go is simply the NULL pointer value of other languages.

You can effectively use it in place of any pointer or interface (interfaces are somewhat pointers).

You can use it as an error, because the error type is an interface.

You can't use it as a string because in Go, a string is a value.

nil is untyped in Go, meaning you can't do that:

var n = nil

Because here you lack a type for n. However, you can do

var n *Foo = nil

Note that nil being the zero value of pointers and interfaces, uninitialized pointers and interfaces will be nil.

like image 45
Elwinar Avatar answered Oct 17 '22 10:10

Elwinar


nil is also a value but only difference is- it is empty.

In Javascript for the un-initialized variable will be undefined. In the same way Golang has nil as default value for all the un-initalized data types.

like image 3
ajaykumar mp Avatar answered Oct 17 '22 11:10

ajaykumar mp


  • For the data types like slice and pointers which "refer" to other types, "nil" indicates that the variable does not refer to an instance (equivalent of null pointer in other languages)
  • It is thee zero value of the variables of type functions, maps, interfaces, channels and structures
like image 1
pr-pal Avatar answered Oct 17 '22 10:10

pr-pal