On Fri, Aug 4, 2023 at 10:33 AM alchemist vk <alchemist...@gmail.com> wrote:

> Hi folks,
>  In below code, I am invoking receiver api show() via a simple uInteger
> type variable instead of pointer, by which it expects to be invoked . Go
> being  strict with type casing and I was expecting a compiler error. But to
> my surprise, it compiled and executed successfully.  Can you folks please
> explain this behavior.
>
>
>
>
>
>
>
>
> *package mainimport "fmt"type uInteger intfunc main() {    var x uInteger
> = 10    x.show()*
>

Above, the variable x is addressable, so the compiler passes &x to show().

To call a pointer receiver method of a variable, that variable has to be
addressable (it doesn't have to be a pointer), so the compiler knows how to
pass the address of it. Here's how it is described in the spec:

https://go.dev/ref/spec#Method_values


>
>
>
>
> *}func (x *uInteger) show() {   fmt.Printf("In show, x = %d\n", *x)}*
>
> Thanks in Advance,
> Venkatesh
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/CAJr0cerqTGvQA56yQWVYW3F2Ms5vbwq3YyO%2But%3DzJ%2BM4rqf81A%40mail.gmail.com
> <https://groups.google.com/d/msgid/golang-nuts/CAJr0cerqTGvQA56yQWVYW3F2Ms5vbwq3YyO%2But%3DzJ%2BM4rqf81A%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAMV2Rqq%2BZi7ioHi4PPXTmQ7du45LRJaEiSSWJpf1zeH%2Bq3Wjeg%40mail.gmail.com.

Reply via email to