I have multiple struct
s share some fields. For example,
type A struct {
Color string
Mass float
// ... other properties
}
type B struct {
Color string
Mass float
// ... other properties
}
I also have a function that only deals with the shared fields, say
func f(x){
x.Color
x.Mass
}
How to deal with such situations? I know we can turn the color and mass into functions, then we can use interface and pass that interface to the function f
. But what if the types of A
and B
cannot be changed. Do I have to define two functions with essentially the same implementation?