3 ms·
Calling a method defined with a pointer receiver on a non-pointer works: https://play.golang.org/p/VDkz9H9mBF https://play.golang.org/p/VDkz9H9mBF
by kevinmgranger 10y ago
Calling a method defined with a pointer receiver on a non-pointer works: https://play.golang.org/p/VDkz9H9mBF https://play.golang.org/p/VDkz9H9mBF
- tiglionabbit 10y agoOh, is that new? I remember this biting me when I wanted to write a function that would interpret the value I passed to it, and have a special case for null pointers. I wanted to also be able to pass a non-pointer to this same function name and get the standard case, but the dispatch would not let me do this unless I gave both versions of the function different names.
- weberc2 10y agoThe gotcha is usually when you're trying to use that type to satisfy an interface. If the methods that satisfy an interface are defined on a pointer, then you need to pass a pointer and not a value.