Hacker News new | past | comments | ask | show | jobs | submit login
Golang: When 'nil' Isn't (polytomic.com)
10 points by gms on March 30, 2022 | hide | past | favorite | 4 comments



> They’re not easy to create without either using the reflect package, or playing with the internal representation of Go variables.

Ouch, when people corrupt the state machine.


No, a simple conversion can create an interface containing a typed nil. No need for the reflect package, neither messing with internal representation: https://go.dev/play/p/HGVNzf8gL-m


Thanks for the reflectless example. It's useful to see how you might get into this situation without reflection.


The sibling comment did a great job of distilling down a reflect-free case of how you might get yourself into this situation.

In part, the reason I modeled it the way I did was because it was the way I discovered what was going on, but also because it shows what exactly is going on under the hood (and why nil, isn't).

It's not inconceivable to get yourself into this case if you're not careful with e.g. scanning variables into interfaces using the sql package. In this case, the machinery produced a semantically correct result (a typed nil), but the result is not very idiomatic in Go.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: