简单了解下 Interface

基本概念

来自于官方文档的说明: An interface type defines a type set . A variable of interface type can store a value of any type that is in the type set of the interface. Such a type is said to implement the interface

翻译:一个接口类型定义一个类型集合。接口类型的变量可以存储接口类型集合中任何类型的值。这样的类型被认为是实现了接口

重点是后面这段话,我们来进行分段解释。

  • 接口类型的变量可以存储接口类型集合中任何类型的值

    这意味着如果你有一个接口,该接口定义了一些方法,那么任何具有这些方法的类型的值都可以被存储在该接口类型的变量中

  • 这样的类型被认为是实现了接口

    在Go中,类型不需要显示声明它实现了某个接口。只要类型具有接口所需的所有方法,它就被视为实现了接口。

案例: 假设我们有以下的接口定义:

go 复制代码
type Duck interface {
    shout() string
}

现在,我们定义了两个类型,它们都有 shout 方法:

go 复制代码
type Dog struct {
    name string
}

func (dg Dog) shout() string {
    return dg.name + " is shouting"
}

type Bird struct {
    name string
}

func (bd Bird) shout() string {
    return bd.name + " is shouting"
}

由于Dog和Bird都有shout方法,他们都实现了Duck接口。因此,我们可以这样做:

go 复制代码
func main() {
    var duck Duck

    duck = Dog{name: "Dog"}
    fmt.Println(duck.shout()) // 输出:Dog is shouting

    duck = Bird{name: "Bird"}
    fmt.Println(duck.shout()) // 输出: Bird is shouting
}

在这个例子中,duck 是一个 Duck 接口类型的变量。尽管Dog和Bird是完全不同的类型,但由于它们都实现了Duck接口,所以它们的实例都可以赋值给duck变量,并调用它们的shout方法。

  • 接口不仅可以存储方法,还可以存储类型元素 我们先来看一个函数
go 复制代码
func speak[T float64 | float32 | int | string | rune | byte] () {}

上述案例声明了一个泛型函数speak,泛型T它表示float64 | float32 | int | string | rune | byte这些类型,如果还想为T增加类型,便还需往后面添加,不美观也不便于检查。那么我们就可以使用interface来表示。

go 复制代码
type GeneralType interface {
    float64 | float32 | int | string | rune | byte
}

func speak[T GeneralType] () {}

空接口

上文我们知道,如果一个类型满足接口的所有方法,该类型被认为实现了接口。当一个接口内没有定义任何方法(空接口),那么每个类型都被认为实现了它,使得它可以代表任何值。

go 复制代码
var anything interface{}

anything = 100
fmt.Println(anything) // 输出:100
anything = "3.141592653589793"
fmt.Println(anything) // 输出:3.141592653589793
anything = true
fmt.Println(anything) // 输出:true

嵌入式接口(Embedded interfaces)

经典套娃环节又来了,Go允许我们在接口内部除了写类型元素和方法,还可以写接口。表示合并了接口内部的所有方法

go 复制代码
type Dog interface {
    walk()
}

type Bird interface {
    shout()
}

type Animal interface {
    Dog()
    Bird()
}

Animal接口嵌套了 Dog和Bird接口,相当于合并了Dog接口和Bird接口内部的所有方法。

相关推荐
小满zs1 天前
Go语言第十八章(架构设计)
后端·go
FungLeo2 天前
成为全栈·Go 后端篇·三轮代码审阅之后:把可读性和证据做成工程规则
golang·go·ast·静态检查·成为全栈·代码审阅
我不会起名字3222 天前
Redis 从 0 到 1(二十八):Redis 抖一下,重试把它打挂了——超时/重试/熔断的 Go 封装
redis·go·超时·熔断·重试
xyzzklk2 天前
变相解决华为Mate 8锁屏后无法建立入站网络连接
linux·运维·服务器·网络·华为·go·安卓
vipxieliang3 天前
语言对比卡:PHP vs Go vs Java 的优缺点、适用场景与「为什么选 PHP」
java·go·php
我不会起名字3224 天前
Redis 从 0 到 1(二十七):JSON 体积是 Protobuf 的 3.7 倍?3 种序列化的 Go 基准测试
redis·go·序列化·protobuf·messagepack
我不会起名字3224 天前
Redis 从 0 到 1(二十三):一个热 key 打到 10 万 QPS——hotkey 与本地缓存的 Go 实现
redis·缓存·go·qps·hotkey
Patrick_Wilson4 天前
Go 为什么需要 defer?从函数怎么收尾说起
后端·go·编程语言
Patrick_Wilson4 天前
从文本输出理解 Go 方法与接口
后端·go·编程语言
Patrick_Wilson5 天前
读懂一个 Go JSON 处理函数:从输入数据追踪结构体、校验与错误返回
go·json·编程语言