The code below is really self-explanatory.
How come I can say that the result of CreateLion(), a pointer to a struct that implements the Cat interface, is an instance of
The problem here is that statically typed go differentiates the "this is a function that returns a cat" from "this is a function that returns a lion that is a cat" And therefore will not accept one as the other.
The way to fix this is to give your factory var exactly what it expects:
var cf CatFactory = func() Cat{
return CreateLion()
}
catlion := cf()
catlion.Meow()