4 ms·
Go also allows you to write nil-processing behaviour in your types: func (t MyType*) process(a int, b int) { ... } ... var example *MyType example.
by mattbee 4y ago
Go also allows you to write nil-processing behaviour in your types:
func (t MyType*) process(a int, b int) { ... }
...
var example *MyType
example.process(1,2)
Will call the process method, even though the receiver is nil!
I'm not sure my brain is quite wired to cope with it yet though.
- ainar-g 4y agoIt can be useful when there is a piece of functionality that could be or not be present depending on the configuration. Something like a debug logger that is nil in a release build or some OS-dependent feature handler that is only non-nil on that particular OS. I do admit, though, that that might be confusing for people coming from languages like C++, where calling methods of null pointers is almost always an error.