今天写代码遇到了一个坑:
package main
import "fmt"
type Person interface {
Say()
Name()
}
type Parent struct {
}
func (s *Parent) Say() {
fmt.Println("i am " + s.Name())
}
func (s *Parent) Name() string {
return "parent"
}
type Child struct {
Parent
}
func (s *Child) Name() string {
return "child"
}
func main() {
var c Child
// i am parent
c.Say()
c1 := &Child{}
// i am parent
c1.Say()
}
这个输出 // i am parent, 感觉 go 的组合真的和继承没有任何关系.