在Go中,您不会像Java,c#等那样使用传统的多态性。大多数事情都是使用合成和类型嵌入完成的。一种简单的方法是更改设计并将公用字段分组到单独的结构中。这只是一种不同的想法。
type Common struct { Color string Mass float32}type A struct { Common // ... other properties}type B struct { Common // ... other properties}func f(x Common){ print(x.Color) print(x.Mass)}//example callsfunc main() { f(Common{}) f(A{}.Common) f(B{}.Common)}使用此处)提到的接口和获取器也有其他方法,但是IMO这是最简单的方法



