This question already has an answer here:
I want to call a method which accepts slice of interface as paramers in golang, but I find that I cannot pass it write as this:
type Base interface {
Run()
}
type A struct {
name string
}
func (a *A) Run() {
fmt.Printf("%s is running
", a.name)
}
func foo1(b Base) {
b.Run()
}
func foo2(s []Base) {
for _, b := range s {
b.Run()
}
}
func TestInterface(t *testing.T) {
dog := &A{name: "a dog"}
foo1(dog)
// cat := A{name: "a cat"}
// foo1(cat)
s := []*A{dog}
foo2(s)
}
I get error like this:
cannot use s (type []*A) as type []Base in argument to foo2
</div>