package main import "fmt" type gameObject struct { name string test } type test struct { v string } func (o *test) Val() { fmt.Printf("test %s \n", o.v) } func (o *test) Attack() { fmt.Printf("test Attack %s \n", o.v) } func (o *gameObject) Name() string { return o.name } func (o *gameObject) Attack() { fmt.Printf("GameObject %s do not know how to attack\n", o.name) } type player struct { gameObject } func (p *player) Attack() { fmt.Printf("player %s attacks\n", p.name) } type monster struct { gameObject } type challenger interface { Name() string Attack() } func main() { p := &player{gameObject: gameObject{name: "devgl", test: test{v: "xxx"}}} m := &monster{gameObject: gameObject{name: "slime", test: test{v: "xxx2"}}} Attack(p) Attack(m) m.Attack() m.Val() m.gameObject.test.Val() m.gameObject.test.Attack() } func Attack(c challenger) { c.Attack() } 代码运行结果: player devgl attacks GameObject slime do not know how to attack GameObject slime do not know how to attack test xxx2 test xxx2 test Attack xxx2 用struct来声明一个结构体 总结: 对于多层嵌套得 struct: 内层struct属性会“嵌入”(embed,继承)到外层; 外层struct属性会覆盖内层struct的相同属性; struct之间有层级关系;