I have trouble finding answer to the following question.
I have an interface and a struct that implements it.
type InterfaceA interface {
DoA()
}
type ImplementsA struct {}
func (a ImplementsA) DoA() {
fmt.Println("do A")
}
The question is: why can't I use a slice of ImplementsA to pass it to a function, that expects a variadic number of InterfaceA arguments?
func UsesVariadicNumberOfA(as ...InterfaceA) {
for _, a := range as {
a.DoA()
}
}
func main() {
a1 := ImplementsA{}
a2 := ImplementsA{}
as := []ImplementsA{a1, a2}
// this does not compile, with "cannot use as (variable of type []ImplementsA) as []InterfaceA [...]"
UsesVariadicNumberOfA(as...)
// this works, of course
ais := []InterfaceA{a1, a2}
UsesVariadicNumberOfA(ais...)
}