Home  >  Article  >  Backend Development  >  What does golang nil mean?

What does golang nil mean?

(*-*)浩
(*-*)浩Original
2019-12-13 13:44:512888browse

What does golang nil mean?

Everyone knows that when you declare a variable but do not assign a value, golang will automatically give your variable type a corresponding default zero value.

This is the zero value corresponding to each type:                             (Recommended learning: go)

bool      -> false                              
numbers -> 0                                 
string    -> ""      

pointers -> nil
slices -> nil
maps -> nil
channels -> nil
functions -> nil
interfaces -> nil

Another strcut:

type Person struct {
  Age int
  Name string
  Friends []Person
}

var p Person // Person{0, "", nil}

The variable p is only declared but not assigned, so all fields of p have corresponding zero values.

1. Go’s documentation says that nil is a predefined identifier that represents the zero value of a pointer, channel, function, interface, mapping or slice. It is not one of the keywords of GO.

2. nil can only be assigned to pointer, channel, func, interface, map or slice type variables (non-basic types), otherwise pani will be triggered

The above is the detailed content of What does golang nil mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to use golang luaNext article:How to use golang lua