Home > Article > Backend Development > 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!