If I have two types:
type A struct {
X int
Y int
}
type B struct {
X int
Y int
Z int
}
Is there any way to achieve the following without needing two methods, given that both access identically-named fields and return the sum of them?
func (a *A) Sum() int {
return a.X + a.Y
}
func (b *B) Sum() int {
return b.X + b.Y
}
Of course, were X and Y methods, I could define an interface containing these two methods. Is there an analogue for fields?