What does nil mean in golang?

Viewed 75420

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?

8 Answers

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.

  • 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

nil in Go means a zero value for pointers, interfaces, maps, slices, and channels. It means the value is uninitialized

nil is a predeclared identifier in Go that represents zero values for pointers, interfaces, channels, maps, slices and function types. nil being the zero value of pointers and interfaces, uninitialized pointers and interfaces will be nil.

In Go, string can’t be nil. A string doesn’t qualify to be assigned nil.

a := nil   // It will throw an error 'use of untyped nil'

var a *int = nil  //It will work

In Go nil is predeclared identifier that represents nothing for pointers, interfaces, channels, maps, slices and function types. Uninitialized pointers and interfaces will be nil. A string doesn’t qualify to be either of the above-mentioned types and hence can’t be assigned nil. If you declare a variable by shorthand decleration like this: variable:=nil this will give compile time error because compiler has no idea which type it has to assign to variable .

In Go map, slice and function doesn't support comparison but can be compared with nil. If any two map, slice or function are assigned to nil and compared with each other then it will raise runtime error. But if we assign nil to any two pointer variables and compare them with each other then no error will occur.

package main

import (
"fmt"
)

 func main() {
    var i *int =nil
    var j *int =nil
    if i==j {
      fmt.Println("equal")
    } else {
      fmt.Println("no")
    }
}

This code works fine and prints output as equal.

In Golang, datatype can use nil is pointers, interfaces, maps, slices, channels, function types and errors

in String value is ""

in INT value is ZERO

Related