如果函数参数是一个接口切片或变参 如何传参
来源:6-6 常用系统接口

qq_慕工程65285
2021-11-09
type Item interface {
a()
}
type item struct {
}
func (i item) a() {
panic("not implemented")
}
func recitems(i ...Item) error {
return nil
}
func getitems() []item {
func main() {
recitems(getitems())
}
cannot use getitems() (value of type []item) as Item value in argument to recitems: missing method a
比如这个例子 item实现了Item接口 但recitems
函数参数是...Item
或者[]Item
怎么传参过去呢
写回答
1回答
-
getitems()后面加...即可。另外,getitems()需要返回[]Item,大写的。
package main
type Item interface {
a()
}
type item struct {
}
func (i item) a() {
panic("not implemented")
}
func recitems(i ...Item) error {
return nil
}
func getitems() []Item {
return []Item{}
}
func main() {
recitems(getitems()...)
}
10
相似问题