I'm reading the spec about selectors: https://golang.org/ref/spec#Selectors
Why q.M0()
is invalid. While p.M0()
is valid and q=p
. Very strange for me.
The relevant source code:
type T0 struct {
x int
}
func (*T0) M0()
type T1 struct {
y int
}
func (T1) M1()
type T2 struct {
z int
T1
*T0
}
func (*T2) M2()
type Q *T2
var t T2 // with t.T0 != nil
var p *T2 // with p != nil and (*p).T0 != nil
var q Q = p
p.M0() // ((*p).T0).M0() M0 expects *T0 receiver
q.M0() // (*q).M0 is valid but not a field selector