go version go1.11.2 linux/amd64
https://play.golang.org/p/kTvcsWkJeaZ
package main
type T1 struct {
}
func (T1) Hello(T1) {
}
type T2 struct {
T1
}
func (T2) Hello(T2) {
}
type T3 T2
type T4 = T2
func main() {
var v_T3 T3
v_T3.Hello(v_T3)
var v_T4 T4
v_T4.Hello(v_T4)
}
prog.go:21:12: cannot use v_T3 (type T3) as type T1 in argument to v_T3.T1.Hello
row 21:I expect call v_T3.Hello
but the actual call v_T3.T1.Hello
row 23:it's ok
type T4 = T2
This gives an additional name T4 to the existing type T2. It does not introduce a new type. In other words, T2 and T4 are always interchangeable. That should explain why the v_T4.Hello(v_T4)
call works. All of the following calls are all synonymous in terms of types (but ignoring that v_T2 and v_T4 are different values):
var v_T2 T2
var v_T4 T4
v_T2.Hello(v_T2)
v_T2.Hello(v_T4)
v_T4.Hello(v_T2)
v_T4.Hello(v_T4)
type T3 T2
This defines a new type T3 that is distinct from all other types. According to the type rules, T3 doesn't inherit T2's methods. But because they have the same underlying type, T3's and T2's fields are the same, including the embedded T1. This in turn promotes T1's methods to T3.