function to return an Interface

前端 未结 4 618
名媛妹妹
名媛妹妹 2021-02-07 08:27

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

4条回答
  •  迷失自我
    2021-02-07 09:14

    I think you should read this blog http://blog.golang.org/laws-of-reflection,it is precise about the relation between variables,types and interfaces.

    In your example *Lion is different with Cat.

    You can correct function CreateLion returns from *Lion to Cat.

提交回复
热议问题