需要一些帮助来理解golang。
来自使用基类C++这是微不足道的。在 Go 中,使用结构组合,它工作正常,直到我需要具有采用“Base”结构的功能。我知道它不是真正的基类,但是当涉及到从派生的基类的字段分配值时,它工作正常。但是我不能传递到一个采取.DogWolf
package main
import "fmt"
type Wolf struct {
ID string
Schema int
}
type Dog struct {
Wolf
}
type Dingo struct {
Wolf
}
func processWolf(wolf Wolf) {
fmt.Println(wolf.ID, wolf.Schema)
}
func main() {
porthos := Dog{}
porthos.ID = "Porthos" // works fine able to set field of Wolf
porthos.Schema = 1 // works fine
aussie := Dingo{}
aussie.ID = "Aussie" // works fine
aussie.Schema = 1 // works fine
fmt.Println(porthos.ID, porthos.Schema)
fmt.Println(aussie.ID, aussie.Schema)
processWolf(porthos) << fails here
processWolf(aussie) << fails here
}
炎炎设计
ITMISS
相关分类