Quantcast
Channel: User Schwern - Stack Overflow
Viewing all articles
Browse latest Browse all 581

Answer by Schwern for Pointer receiver methods are getting called on interfaces holding values

$
0
0

Your pointer String method is not being called. That is expected behavior, pointer methods cannot be called on values for the reasons you gave.

{old 12} is the default output for a struct. See fmt Printing...

struct:             {field0 field1 ...}

Perhaps confusingly, fmt.Println(u1.String()) will work. This is because Go has quietly changed that to fmt.Println(&u1.String()) for your convenience. From Effective Go: Methods, Pointers vs. Values...

There is a handy exception, though. When the value is addressable, the language takes care of the common case of invoking a pointer method on a value by inserting the address operator automatically.


Viewing all articles
Browse latest Browse all 581

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>