深入了解Golang interface{}的底层原理实现
来源:脚本之家
时间:2022-12-22 21:45:08 230浏览 收藏
本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《深入了解Golang interface{}的底层原理实现》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~
interface数据结构
golang中的接口分为带方法的接口和空接口。 带方法的接口在底层用iface表示,空接口的底层则是eface表示。下面咱们透过底层分别看一下这两种数据结构。
iface
iface表示的是包含方法的interface;例如:
type Test interface{
test()
}
底层源代码是:
//runtime/runtime2.go
//非空接口
type iface struct {
tab *itab
data unsafe.Pointer //data是指向真实数据的指针
}
type itab struct {
inter *interfacetype //描述接口自己的类型
_type *_type //接口内部存储的具体数据的真实类型
link *itab
hash uint32 // copy of _type.hash. Used for type switches.
bad bool // type does not implement interface
inhash bool // has this itab been added to hash?
unused [2]byte
fun [1]uintptr // fun是指向方法集的指针。它用于动态调度。
}
//runtime/type.go
type _type struct {
size uintptr
ptrdata uintptr // size of memory prefix holding all pointers
hash uint32
tflag tflag
align uint8
fieldalign uint8
kind uint8
alg *typeAlg
// gcdata stores the GC type data for the garbage collector.
// If the KindGCProg bit is set in kind, gcdata is a GC program.
// Otherwise it is a ptrmask bitmap. See mbitmap.go for details.
gcdata *byte
str nameOff
ptrToThis typeOff
}
和eface相同的是都有指向具体数据的指针data字段。 不同的是_type变成了tab。
- hash 是对 _type.hash 的拷贝,当我们想将 interface 类型转换成具体类型时,可以使用该字段快速判断目标类型和具体类型 runtime._type 是否一致;
- fun 是一个动态大小的数组,它是一个用于动态派发的虚函数表,存储了一组函数指针。虽然该变量被声明成大小固定的数组,但是在使用时会通过原始指针获取其中的数据;

eface
eface顾名思义 empty interface,代表的是不包含方法的interface,例如:
type Test interface {}
因为空接口不包含任何方法,所以它的结构也很简单,底层源代码是:
//空接口
type eface struct {
_type *_type //接口内部存储的具体数据的真实类型
data unsafe.Pointer //data是指向真实数据的指针
}
//runtime/type.go
type _type struct {
size uintptr
ptrdata uintptr // size of memory prefix holding all pointers
hash uint32
tflag tflag
align uint8
fieldalign uint8
kind uint8
alg *typeAlg
// gcdata stores the GC type data for the garbage collector.
// If the KindGCProg bit is set in kind, gcdata is a GC program.
// Otherwise it is a ptrmask bitmap. See mbitmap.go for details.
gcdata *byte
str nameOff
ptrToThis typeOff
}
eface 结构体主要包含两个字段,共16字节。
- _type:这个是运行时 runtime._type 指针类型,表示数据类型
- data: 表示的数据指针

总结
Go语言底层对非空interface和空interface实现上做了区分。空interface的底层结构是eface结构。它与iface的区别在于,它拥有的不再是 *itab类型数据,而是 _type 类型的数据。是因为,eface面向的是空的interface数据,既然是空的interface,那么只需要用 *_type 代表类型,data字段指向具体数据即可。这里的 *_type 是此interface代表的数据的类型。
文中关于golang的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《深入了解Golang interface{}的底层原理实现》文章吧,也可关注golang学习网公众号了解相关技术文章。
-
288 收藏
-
394 收藏
-
401 收藏
-
406 收藏
-
369 收藏
-
110 收藏
-
412 收藏
-
423 收藏
-
274 收藏
-
379 收藏
-
241 收藏
-
235 收藏
-
365 收藏
-
247 收藏
-
241 收藏
-
467 收藏
-
500 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习