I have an object parsed from some XML file. It has struct type like this
type Report struct {
Items []Item `xml:......`
AnotherItems []AnotherItem `xml:......`
}
type Item struct {
Name string
}
type AnotherItem struct {
Name string
}
func (Item *Item) Foo() bool {
//some code here
}
func (AnotherItem *AnotherItem) Foo() bool {
//another code here
}
For each item i have to do smth like this:
func main(){
//some funcs called to get the report object
doSmth(report.Items)
doSmth(report.AnotherItems)
}
func doSmth(items ????){
for _, item : range items {
item.Foo()
}
}
Since i have different items with same function i want to have only one doSmth
so i can't do just doSmth(items []Item)
And the question is - what should i write instead of "????" to get this working?
The only way i made it to pass report.Items into doSmth() was
func doSmth(items interface{})
But it throws me an error "cannot range over items (type interface {})" And if instead of iteration i just put smth like
func doSmth(items interface{}){
fmt.Println(items)
}
program print the list of my items