,我被弄得实验用下面的程序,涉及到履行界面结构嵌入,与命名的类型和指针接收器,分别满足:接口通过结构嵌入
package main
import "fmt"
type MyInt interface {
mytest()
}
type Base struct {
}
func (b *Base) mytest() {
fmt.Println("From base")
}
type Derived struct {
Base
}
type Derived2 struct {
*Base
}
func main() {
// Only this one has problem
// However, if we change mytest's receiver from *Base to Base, all the four assignments are OK
var _ MyInt = Derived{}
// OK
var _ MyInt = &Derived{}
var _ MyInt = Derived2{}
var _ MyInt = &Derived2{}
}
见注释代码为了我的困惑。有没有解释它们的主要方法?
这解释了它,但我不明白这些规则背后的动机。 –
@ M.Tong我添加了一段话来解释我认为动机是什么。 –