3 ms·
'nil' in Go is just an identifier to represent the zero values of some kinds of type. A bare nil is always untyped and it is the only untyped value which has no
by tapirl 6y ago
'nil' in Go is just an identifier to represent the zero values of some kinds of type. A bare nil is always untyped and it is the only untyped value which has not a default type.
Zero values behave the same as non-zero values in many aspects.
More read: nils in Go https://go101.org/article/nil.html https://go101.org/article/nil.html
The facts that several kinds of type share the same zero value literal representation really confuse many new gophers, but this is hardly a problem when you get familiar with the design.
There are proposals to remove the new-gopher confusion.
* https://github.com/golang/go/issues/22729 https://github.com/golang/go/issues/22729 use different zero identifiers for different kinds of type
* I remembered that there is a propsoal which propose using `null` for non-interface zero values but still using `nil` for interfaces. I couldn't find this issue now.