On Wed, Mar 16, 2022 at 3:25 PM Alex Besogonov <alex.besogo...@gmail.com>
wrote:

> Note that in your case, the dynamic value of that interface *is not nil*.
> It's a struct value. Structs can not be nil.
>
> Incorrect. Go language allows coercing a struct with an embedded pointer
> to an interface. Spec:
> "If S contains an embedded field *T, the method sets of S and *S both
> include promoted methods with receiver T or *T."
>

Consider this code:

interface Fooable { Foo() }
type T struct {}
func (*T) Foo() {}
type S struct { *T }
func bar(f Fooable) { f.Foo() }
func baz() {
   var s S
   bar(s)
}

As far as I can tell, you seem to think that the call bar(s) is equivalent
to bar(s.T). In other words, what is passed to bar is an interface value
containing a copy of s.T. This is not correct.

What actually happens is equivalent to the compiler generating a method

var (s S) Foo() { s.T.Foo() }

so that S actually implements the interface Fooable. And the call bar(s)
passes to bar an interface value containing a copy of s itself.

-- 
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/CAADvV_u5YPQRx0SJKXzDccd6mdC-xvtmzs5zjPH5pmNREN9TQA%40mail.gmail.com.

Reply via email to