Example:

package main import ( "fmt" ) type A interface { Foo() } type B struct{} func (b *B) Foo() { // do smth... } func foo(b []*B) []A { return b // cannot use b (type []*B) as type []A in return argument } func main() { b := make([]*B, 10) a := foo(b) fmt.Println(a) } 

Interface A is fully satisfied with type *B , then why can not I return from function []*B as []A , and even explicit type conversion does not save? Where is my polymorphism? And is there any way to turn []*B into []A , except

 aa := make([]A, 0, len(b)) for _, v := range b { aa = append(aa, v) } 

    1 answer 1

    It turns out that Google has already answered my question. And in this case, it turns out that without an explicit re-creation of the array, there is no possibility on the fly to convert a slice of a certain type into a slice of interfaces.