Interface

Go Interface是什么?

简单来说,是一组方法的集合,也是一种类型
Go允许没有任何方法的interface,称为空interface,可以认为任何的类型都实现了空interface
如果一个类型A实现了如下Interface GetSay的方法,我们就认为A实现了该Interface,在实际的函数调用传参中A是可以直接作为GetSay类型的参数,也可以理解为多态

type Animals interface {
      GetSay(ctx context.Context, id string) (int, error)
      GetWalk(ctx context.Context, id string) error
}

Go Interface能做什么?

Go 如何使用Interface?

posted @ 2020-09-20 15:44  ruixing  阅读(251)  评论(0编辑  收藏  举报