I have a type T which embed type B, and *B implements I. *T can be assigned to a variable of type I but not in the case of T, does this mean (*T)'s method set contains both value and pointer receiver of B?
package main
import (
"fmt"
)
type I interface {
Foo()
}
type B struct {}
type T struct {
B
}
func (a *B) Foo() {
fmt.Println("Bar")
}
func main() {
t := T{B{}}
// var i I = t -> error
var i I = &t
i.Foo()
}
Yes, method set of *T
contains methods with receiver B
and *B
.
Given a struct type
S
and a defined typeT
, promoted methods are included in the method set of the struct as follows:
- If
S
contains an embedded fieldT
, the method sets ofS
and*S
both include promoted methods with receiverT
. The method set of*S
also includes promoted methods with receiver*T
.- If
S
contains an embedded field*T
, the method sets ofS
and*S
both include promoted methods with receiverT
or*T
.