我尝试附加可以用同一接口表示的不同函数。函数返回不同的对象但相同的接口。它因错误而失败cannot use Test (value of type func() *Dog) as func() Animal value in argument to append (typecheck) 我该怎么办?提前致谢!
package main
type Dog struct {
Word string
}
type Cat struct {
Word string
}
func (d *Dog) Say() string {
return d.Word
}
func (c *Cat) Say() string {
return c.Word
}
type Animal interface {
Say() string
}
func main() {
funcs := []func() Animal{}
funcs = append(funcs, Test) // error| cannot use Test (value of type func() *Dog) as func() Animal value in argument to append (typecheck)
}
func Test() *Dog {
return &Dog{Word: "dog"}
}
func Test2() *Cat {
return &Cat{Word: "cat"}
}
四季花海
忽然笑
慕少森
相关分类