我对以下程序的实验感到困惑,这些程序分别与使用结构嵌入实现接口、命名类型和指针接收器有关:
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{}
}
请参阅代码中的注释以了解我的困惑。有什么主要的方法来解释它们吗?
慕码人8056858
慕容3067478
相关分类