【发布时间】:2016-05-09 11:11:57
【问题描述】:
我有一个从某个 XML 文件中解析出来的对象。它有这样的结构类型
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
}
对于每个项目,我都必须这样做:
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()
}
}
由于我有具有相同功能的不同项目,我只想拥有一个doSmth,所以我不能只做doSmth(items []Item)
问题是 - 我应该写什么而不是“??????”让这个工作?
我将 report.Items 传递给 doSmth() 的唯一方法是
func doSmth(items interface{})
但它给我一个错误“无法覆盖项目(类型接口 {})” 如果不是迭代,我只是把 smth 像
func doSmth(items interface{}){
fmt.Println(items)
}
程序打印我的项目列表
【问题讨论】:
-
你试过
(items ...)吗? -
编译器说
final argument in variadic function missing type
标签: go