当前位置:首页 > 文章列表 > Golang > Go教程 > Go interface 接口的最佳实践经验分享

Go interface 接口的最佳实践经验分享

来源:脚本之家 2023-02-24 21:54:40 0浏览 收藏

亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《Go interface 接口的最佳实践经验分享》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下接口、interface,希望所有认真读完的童鞋们,都有实质性的提高。

Go语言-Go 接口的最佳实践

原文连接:https://blog.boot.dev/golang/golang-interfaces/

Go 中的接口允许我们暂时将不同的类型视为相同的数据类型,因为这两种类型实现相同的行为。它们是Go程序员工具箱的核心,并且经常被新的Go开发人员不正确地使用,导致代码不可读且经常有错误。

什么是Golang中的interface

In Go, an interface is a custom type that other types are able to implement, which gives Go developers a powerful way to use abstraction. Interfaces are named collections of method signatures, and when other types implement all the required methods, they implicitly implement the interface.

在 Go 中,接口是其他类型可以实现的自定义类型,这为 Go 开发人员提供了使用抽象的强大方式。接口是方法签名的集合,当其他类型实现所有需要的方法时,它们隐式地实现了接口。

例如,Go 中的errors是接口,标准error接口很简单,一个类型要被认为是error,所需要做的就是定义一个 Error ()方法,该方法不接受任何参数,并返回一个字符串。

type error interface {
    Error() string
}

错误error的简单性使得编写日志和metrics 实现更加容易。让我们定义一个表示网络问题的结构体:

type networkProblem struct {
	message string
	code    int
}

然后我们可以定义一个 Error ()方法:

func (np networkProblem) Error() string {
	return fmt.Sprintf("network error! message: %s, code: %v", np.message, np.code)
}

现在,我们可以在接受错误的任何地方使用 networkProblem struct 的实例。

func handleErr(err error) {
	fmt.Println(err.Error())
}
np := networkProblem{
	message: "we received a problem",
	code:    404,
}
handleErr(np)
// prints "network error! message: we received a problem, code: 404"

编写接口的最佳实践

编写干净的接口是困难的。坦率地说,任何时候你都在处理代码中的抽象,如果你不小心的话,简单可以很快变成复杂。让我们回顾一下保持interfaces整洁的一些经验法则。

  • Keep interfaces small 保持interfaces足够小
  • Interfaces should have no knowledge of satisfying types 接口应该没有令人满意的类型的知识
  • Interfaces are not classes 接口不是类

1. 保持interfaces足够小

If there is only one piece of advice that you take away from this article, make it this: keep interfaces small! Interfaces are meant to define the minimal behavior necessary to accurately represent an idea or concept.

如果您从本文中只得到了一条建议,那就是: 保持接口小一些!接口意味着定义精确表示一个想法或概念所需的最小行为。

下面是一个大型接口的标准 HTTP package的例子,它是定义最小行为的一个很好的例子:

type File interface {
    io.Closer
    io.Reader
    io.Seeker
    Readdir(count int) ([]os.FileInfo, error)
    Stat() (os.FileInfo, error)
}

Any type that satisfies the interface’s behaviors can be considered by the HTTP package as a File. This is convenient because the HTTP package doesn’t need to know if it’s dealing with a file on disk, a network buffer, or a simple []byte.

任何满足接口行为的类型都可以被 HTTP package 视为File。这很方便,因为 HTTP package 不需要知道它是在处理磁盘上的文件、还是网络缓冲区或是[]byte

2. Interfaces Should Have No Knowledge of Satisfying Types

An interface should define what is necessary for other types to classify as a member of that interface. They shouldn’t be aware of any types that happen to satisfy the interface at design time.

接口应该定义其他类型作为该接口的成员所必需的内容。他们不应该知道在设计时为了满足接口而发生的任何类型。

例如,假设我们正在构建一个接口来描述定义汽车所必需的构成元素。

type car interface {
	Color() string
	Speed() int
	IsFiretruck() bool
}

Color() and Speed() make perfect sense, they are methods confined to the scope of a car. IsFiretruck() is an anti-pattern. We are forcing all cars to declare whether or not they are firetrucks. In order for this pattern to make any amount of sense, we would need a whole list of possible subtypes. IsPickup(), IsSedan(), IsTank()… where does it end??

Color()Speed()非常合理,它们是限制在汽车范围内的方法。IsFiretruck ()是一个反模式。我们正在强制所有的汽车申报它们是否是消防车。为了使这个模式具有任何意义,我们需要一个可能的子类型的完整列表。IsPickup () ,IsSedan () ,IsTank () … 它在哪里结束?

Instead, the developer should have relied on the native functionality of type assertion to derive the underlying type when given an instance of the car interface. Or, if a sub-interface is needed, it can be defined as:

相反,当给定汽车接口的实例时,开发人员应该依赖于类型断言的原生功能来派生基础类型。或者,如果需要子接口,可以将其定义为:

type firetruck interface {
	car
	HoseLength() int
}

它继承了汽车所需的方法,并增加了一个额外的所需方法,使汽车一辆消防车。

3. 接口不是类

  • Interfaces are not classes, they are slimmer. 接口不是类,它们更小
  • Interfaces don’t have constructors or deconstructors that require that data is created or destroyed. 接口没有要求创建或销毁数据的构造函数或解构函数
  • Interfaces aren’t hierarchical by nature, though there is syntactic sugar to create interfaces that happen to be supersets of other interfaces. 接口本质上并不具有层次性,尽管在创建恰好是其他接口的超集的接口时存在语法糖
  • Interfaces define function signatures, but not underlying behavior. Making an interface often won’t 接口定义函数签名,但不定义底层行为。制作interface通常不会在结构方法方面不干扰您的代码。例如,如果五种类型满足错误接口,那么它们都需要自己的版本的Error() function. 函数

有关接口的更多信息

空的接口

空接口没有指定任何方法,因此 Go 中的每个类型都实现了空接口。

interface{}

It’s for this reason that developers sometimes use a map[string]interface{} to work with arbitrary JSON data, although I recommend using anonymous structs instead where possible.

出于这个原因,开发人员有时使用 map[string]interface{}来处理任意 JSON 数据,尽管我推荐在可能的情况下使用匿名结构

Zero value of an interface

Interfaces can be nil, in fact, it’s their zero value. That’s why when we check for errors in Go, we’re always checking if err != nil, because err is an interface.

接口可以是 nil,事实上,这是它们的零值。这就是为什么当我们在 Go 中检查错误时,我们总是检查err != nil,因为 err 是一个接口。

指针上的接口

It’s a common “gotcha” in Go to implement a method on a pointer type and expect the underlying type to implement the interface, it doesn’t work like that.

在 Go 中,在指针类型上实现一个方法并期望底层类型实现接口是一个常见的“明白了”,它不是这样工作的。

type rectangle interface {
    height() int
    width() int
}

type square struct {
    length int
}

func (sq *square) width() int {
    return sq.length
}

func (sq *square) height() int {
    return sq.length
}

Though you may expect it to, in this example the square type does not implement the rectangle interface. The *square type does. If I wanted the square type to implement the rectangle interface I would just need to remove the pointer receivers.

虽然您可能希望这样做,但是在这个示例中,正方形类型不实现矩形接口。它使用的是*square。如果我想让正方形类型实现矩形接口,我只需要删除指针接收器。

type rectangle interface {
    height() int
    width() int
}

type square struct {
    length int
}

func (sq square) width() int {
    return sq.length
}

func (sq square) height() int {
    return sq.length
}

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

版本声明
本文转载于:脚本之家 如有侵犯,请联系study_golang@163.com删除
go语言方法集为类型添加方法示例解析go语言方法集为类型添加方法示例解析
上一篇
go语言方法集为类型添加方法示例解析
go语言数组及结构体继承和初始化示例解析
下一篇
go语言数组及结构体继承和初始化示例解析
查看更多
最新文章
查看更多
课程推荐
  • 前端进阶之JavaScript设计模式
    前端进阶之JavaScript设计模式
    设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
    542次学习
  • GO语言核心编程课程
    GO语言核心编程课程
    本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
    508次学习
  • 简单聊聊mysql8与网络通信
    简单聊聊mysql8与网络通信
    如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
    497次学习
  • JavaScript正则表达式基础与实战
    JavaScript正则表达式基础与实战
    在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
    487次学习
  • 从零制作响应式网站—Grid布局
    从零制作响应式网站—Grid布局
    本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
    484次学习
查看更多
AI推荐
  • 笔灵AI生成答辩PPT:高效制作学术与职场PPT的利器
    笔灵AI生成答辩PPT
    探索笔灵AI生成答辩PPT的强大功能,快速制作高质量答辩PPT。精准内容提取、多样模板匹配、数据可视化、配套自述稿生成,让您的学术和职场展示更加专业与高效。
    10次使用
  • 知网AIGC检测服务系统:精准识别学术文本中的AI生成内容
    知网AIGC检测服务系统
    知网AIGC检测服务系统,专注于检测学术文本中的疑似AI生成内容。依托知网海量高质量文献资源,结合先进的“知识增强AIGC检测技术”,系统能够从语言模式和语义逻辑两方面精准识别AI生成内容,适用于学术研究、教育和企业领域,确保文本的真实性和原创性。
    22次使用
  • AIGC检测服务:AIbiye助力确保论文原创性
    AIGC检测-Aibiye
    AIbiye官网推出的AIGC检测服务,专注于检测ChatGPT、Gemini、Claude等AIGC工具生成的文本,帮助用户确保论文的原创性和学术规范。支持txt和doc(x)格式,检测范围为论文正文,提供高准确性和便捷的用户体验。
    30次使用
  • 易笔AI论文平台:快速生成高质量学术论文的利器
    易笔AI论文
    易笔AI论文平台提供自动写作、格式校对、查重检测等功能,支持多种学术领域的论文生成。价格优惠,界面友好,操作简便,适用于学术研究者、学生及论文辅导机构。
    38次使用
  • 笔启AI论文写作平台:多类型论文生成与多语言支持
    笔启AI论文写作平台
    笔启AI论文写作平台提供多类型论文生成服务,支持多语言写作,满足学术研究者、学生和职场人士的需求。平台采用AI 4.0版本,确保论文质量和原创性,并提供查重保障和隐私保护。
    35次使用
微信登录更方便
  • 密码登录
  • 注册账号
登录即同意 用户协议隐私政策
返回登录
  • 重置密码