3 ms·
First and foremost, thanks for this nice description of interfaces! If you understand that an interface value is two words wide and it contains a pointer to th
by danieldk 14y ago
First and foremost, thanks for this nice description of interfaces!
If you understand that an interface value is two words wide and it contains a pointer to the underlying data, that’s typically enough to avoid common pitfalls.
Except for one: namely, if a variable v that has an interface as its type and the value pointer is nil and the type pointer isn't, then v != nil. In other words, if an interface value is internally represented as:
{*Type, *Value}
Then:
{&DogType, nil} != nil
{nil, nil} == nil
This can lead to results that are initially surprising. For a description and an example:
http://golang.org/doc/go_faq.html#nil_error http://golang.org/doc/go_faq.html#nil_error
- drbawb 14y agoSomeone posted this chart[0] on the mailing list[1]. It pretty-prints a chart that shows the different nil comparisons, and what they result in (true, false, or n/a being an error caught by the compiler). [0]: http://play.golang.org/p/7GohUdfIsD http://play.golang.org/p/7GohUdfIsD [1]: https://groups.google.com/d/topic/golang-nuts/zvqvEZk53Q0/discussion https://groups.google.com/d/topic/golang-nuts/zvqvEZk53Q0/di...