简单了解下 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"
}

由于DogBird都有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 接口类型的变量。尽管DogBird是完全不同的类型,但由于它们都实现了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接口嵌套了 DogBird接口,相当于合并了Dog接口和Bird接口内部的所有方法。

相关推荐
刀法如飞5 小时前
【Go 字符串查找的 20 种实现方式,用不同思路解决问题】
人工智能·算法·go
止语Lab10 小时前
别急着拆微服务:Go 项目演进的三个关键决策
go
feasibility.1 天前
反爬十层妖塔:现代爬虫攻防的立体战争
爬虫·python·科技·scrapy·rust·go·硬件
用户398346161201 天前
Go-Spring 实战第 1 课 —— 统一配置模型:Properties 与 Path
go
王中阳Go1 天前
秒杀、分库分表、全链路追踪:一个电商微服务的架构全拆解
后端·go
漓漾li2 天前
每日面试题(2026-05-15)
架构·go·agent
tyung2 天前
用 Go 实现一个生产级 Ring Buffer Queue:环形数组、位运算取模、批量操作全拆解
数据结构·go
Wy_编程2 天前
golang 基础语法和函数
开发语言·go
养肥胖虎2 天前
Docker学习笔记:后端、数据库和反向代理怎么一起跑起来
后端·nginx·docker·postgresql·go·部署