,我被弄得實驗用下面的程序,涉及到履行界面結構嵌入,與命名的類型和指針接收器,分別滿足:接口通過結構嵌入
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我添加了一段話來解釋我認爲動機是什麼。 –