golang接口

接口简介

golang 中接口是常用的数据结构,接口可以实现like的功能。什么叫like呢?
比如麻雀会飞,老鹰会飞,他们都是鸟,鸟有翅膀可以飞。飞机也可以飞,
飞机就是像鸟一样,like bird, 所以我们可以说飞机,气球,苍蝇都像鸟一样可以飞翔。
但他们不是鸟,那么对比继承的关系,老鹰继承自鸟类,它也会飞,但他是鸟。
先看一个接口定义

type Bird interface {
	Fly() string
}

定义了一个Bird类型的interface, 内部声明了一个Fly方法,参数为空,返回值为string。
接口声明方法和struct不同,接口的方法写在interface中,并且不能包含func和具体实现。
另外interface内部不能声明成员变量。
下面去实现蝴蝶类和飞机类,实现like-bird的功能。像鸟一样飞。

type Plane struct {
	name string
}

func (p *Plane) Fly() string {
	fmt.Println(p.name, " can fly like a bird")
	return p.name
}

type Butterfly struct {
	name string
}

func (bf *Butterfly) Fly() string {
	fmt.Println(bf.name, " can fly like a bird")
	return bf.name
}

实现了Plane和Butterfly类,并且实现了Fly方法。那么飞机和蝴蝶就可以像鸟一样飞了。
我们在主函数中调用

pl := &Plane{name: "plane"}
pl.Fly()

bf := &Butterfly{name: "butterfly"}
   bf.Fly()

输出如下

plane  can fly like a bird
butterfly  can fly like a bird

有人会问,单独实现Plane和Butterfly不就好了,为什么要和Bird扯上关系呢?
因为接口作为函数形参,可以接受不同的实参类型,只要这些实参实现了接口的方法,
都可以达到动态调用不同实参的方法。

func FlyLikeBird(bird Bird) {
    bird.Fly()
}

下面我们在main函数中调用上面这个函数,传入不同的实参

FlyLikeBird(pl)
FlyLikeBird(bf)

输出如下

plane  can fly like a bird
butterfly  can fly like a bird

这样就是实现了动态调用。有点类似于C++的多态,golang又不是通过继承达到这个效果的,
只要结构体实现了接口的方法就可以转化为接口类型。
golang这种实现机制突破了Java,C++等传统静态语言显式继承的弊端。

接口类型转换和判断

struct类型如果实现了接口方法,可以赋值给对应的接口类型,接口类型同样可以转化为struct类型。
我们再写一个函数,通过该函数内部将bird接口转化为不同的类型,从而打印具体的传入类型。

func GetFlyType(bird Bird) {
	_, ok := bird.(*Butterfly)
	if ok {
		fmt.Println("type is *butterfly")
		return
	}

	_, ok = bird.(*Plane)
	if ok {
		fmt.Println("type is *Plane")
		return
	}

	fmt.Println("unknown type")
}

main函数调用

func main() {
	pl := &Plane{name: "plane"}
    bf := &Butterfly{name: "butterfly"}
	GetFlyType(pl)
    GetFlyType(bf)
}

输出如下

type is *Plane
type is *butterfly

看得出来接口也是可以转化为struct的。
结构体变量, bool类型:=接口类型.(结构体类型)
bool类型为false说明不能转化,true则能转化。

万能接口interface{}

golang 提供了万能接口, 类型为interface{}, 任何具体的结构体类型都能转化为该类型。我们将之前判断类型的例子
稍作修改。定义Human类和Human的Walk方法,然后实现另一个判断函数,参数为interface{}

type Human struct {
}

func (*Human) Walk() {

}

func GetFlyType2(inter interface{}) {
	_, ok := inter.(*Butterfly)
	if ok {
		fmt.Println("type is *butterfly")
		return
	}

	_, ok = inter.(*Plane)
	if ok {
		fmt.Println("type is *Plane")
		return
	}
	_, ok = inter.(*Human)
	if ok {
		fmt.Println("type is *Human")
		return
	}
	fmt.Println("unknown type")
}

在main函数中调用,我们看看结果

func main() {
	pl := &Plane{name: "plane"}
    bf := &Butterfly{name: "butterfly"}
    hu := &Human{}
	GetFlyType2(pl)
    GetFlyType2(bf)
    GetFlyType2(hu)
}

看到输出

type is *Plane
type is *butterfly
type is *Human

.(type)判断具体类型

接口还提供了一个功能,通过.(type)返回具体类型,但是.(type)只能用在switch中。
我们实现另一个版本的类型判断

func GetFlyType3(inter interface{}) {
	switch inter.(type) {
	case *Butterfly:
		fmt.Println("type is *Butterfly")
	case *Plane:
		fmt.Println("type is *Plane")
	case *Human:
		fmt.Println("type is *Human")
	default:
		fmt.Println("unknown type ")
	}
}

main函数中调用这个函数

GetFlyType3(pl)
GetFlyType3(bf)
GetFlyType3(hu)

输出结果如下

type is *Plane
type is *Butterfly
type is *Human

所以.(type)也实现了类型转换

接口转换注意事项

当一个结构体实现了接口的方法,这里仅仅是结构体类型实现了该方法,结构体指针并未实现该方法,他作为实参传递给interface形参时,interface形参可以动态转换判断是否时结构体类型还是结构体指针类型。但是当仅仅结构体指针实现了接口的方法,那interface无法动态转化为结构体类型做判断,编译器会报错。

先看正常的例子

package main

import (
	"fmt"
)

type Bird interface {
	Fly() string
}

type Plane struct {
	Name string
}

func (plane Plane) Fly() string {
	fmt.Println("plane fly")
	return "plane fly"
}

func CheckFly(bird Bird) {
	plane_p, b := bird.(*Plane)
	if b {
		fmt.Println("plane pointer convert success, is ", plane_p)
	}

	plane, b := bird.(Plane)
	if b {
		fmt.Println("plane conver success, is ", plane)
	}
}

func main() {
	plane := Plane{Name: "plane"}
	CheckFly(plane)

	plane_p := &Plane{Name: "plane_p"}
	CheckFly(plane_p)
}

Plane实现了Bird的Fly方法,所以CheckFly的形参bird可以通过转换判断外界传递的具体类型

这是一个关于 Go 语言接口实现的常见误解。实际上,在 Go 中,当一个类型 T 实现了某个接口 I 的所有方法时,类型 *T 也隐式地实现了接口 I,反之亦然。这是因为接口方法的实现是基于方法集的,而方法集的规则允许这种行为。

即使 *Plane 没有直接实现 Fly 方法,由于 Plane 类型通过值接收者实现了 Fly 方法,根据 Go 语言的方法集规则,*Plane 也隐式地实现了 Bird 接口。因此,类型断言可以成功地将 Bird 接口变量转换为 *Plane 类型的指针。

输出如下

plane conver success, is  {plane}
plane pointer convert success, is  &{plane_p}

如果我们只实现了*Plane的Fly,那CheckFly能否在内部做类型转换判断呢

看这个例子

package main

import (
	"fmt"
)

type Bird interface {
	Fly() string
}

type Plane struct {
	Name string
}

func (plane *Plane) Fly() string {
	fmt.Println("plane fly")
	return "plane fly"
}

func CheckFly(bird Bird) {
	plane_p, b := bird.(*Plane)
	if b {
		fmt.Println("plane pointer convert success, is ", plane_p)
	}

	plane, b := bird.(Plane)
	if b {
		fmt.Println("plane conver success, is ", plane)
	}
}

func main() {
	plane := Plane{Name: "plane"}
	CheckFly(plane)

	plane_p := &Plane{Name: "plane_p"}
	CheckFly(plane_p)
}

编译报错

 Plane does not implement Bird (Fly method has pointer receiver)

也就是说当只有结构体指针*Plane实现Fly方法时,外部函数无法通过接口转化为Plane的方式判断是否为Plane类型。

也就是说当我们用结构体指针类型实现接口方法后,interface只能转化为结构体指针类型。结构体指针的转化范围更小。

这个不用过分记忆,我们实际开发中就是通过结构体指针实现方法,interface也转化为结构体指针类型即可。而且结构体指针有很多好处,传递数据只用四字节,外部函数形参为interface可以校验传入的实参是否为指针类型,达到类型检查的目的,更安全。

这样接口基础都介绍完毕了,下一篇介绍接口实现和剖析。

热门评论

热门文章

  1. 解密定时器的实现细节

    喜欢(566) 浏览(2791)
  2. C++ 类的继承封装和多态

    喜欢(588) 浏览(3883)
  3. Linux环境搭建和编码

    喜欢(594) 浏览(8708)
  4. windows环境搭建和vscode配置

    喜欢(587) 浏览(2056)
  5. slice介绍和使用

    喜欢(521) 浏览(2092)

最新评论

  1. asio多线程模型IOServicePool Lion:线程池一定要继承单例模式吗
  2. 泛型算法的定制操作 secondtonone1:lambda和bind是C11新增的利器,善于利用这两个机制可以极大地提升编程安全性和效率。
  3. 类和对象 陈宇航:支持!!!!
  4. C++ 虚函数表原理和类成员内存分布 WangQi888888:class Test{ int m; int b; }中b成员是int,为什么在内存中只占了1个字节。不应该是4个字节吗?是不是int应该改为char。这样的话就会符合图上说明的情况
  5. 解决博客回复区被脚本注入的问题 secondtonone1:走到现在我忽然明白一个道理,无论工作也好生活也罢,最重要的是开心,即使一份安稳的工作不能给我带来事业上的积累也要合理的舍弃,所以我还是想去做喜欢的方向。

个人公众号

个人微信