I'm building a type structure that represents some devices on my network. There are lots of types of these devices, such as:
type Foo struct { ... }
type Bar struct { ... }
type Spam struct { ... }
but they all share a few common fields, one of which is IP
. I'd like to define Device
as an interface so I can group these together logically
type Device interface {
IP() string
...
}
type LogicalGrouping struct {
Devices []Device
...
}
but I'm running into an error with overlapping names.
func (f *Foo) IP() string { return f.IP } // same method name as field name
I can rename either the field or the method or both, but surely this must be a common use case -- grouping structs by the fields they have in common? Is there an idiomatic solution here?