深入了解Golang interface{}的底层原理实现
作者:1个俗人 发布时间:2024-05-05 09:31:13
标签:Golang,interface
前言
在 Go
语言没有泛型之前,接口可以作为一种替代实现,也就是万物皆为的 interface
。那到底 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: 表示的数据指针
来源:https://juejin.cn/post/7157625818456260644


猜你喜欢
- 本文实例讲述了python实现数独算法的方法。分享给大家供大家参考。具体如下:# -*- coding: utf-8 -*-'
- 1.双击setup.exe.(出现安装向导界面) 2.在安装向导界面:选"基本安装",并选好主目录位置;创建启动数据库(
- PostgreSQL引进“分区”表特性,解放了之前采用“表继承”+“触发器”来实现分区表的繁琐、低效。而添加分区,都是手动执行SQL。演示目
- 本文实例讲述了Python利用神经网络解决非线性回归问题。分享给大家供大家参考,具体如下:问题描述现在我们通常使用神经网络进行分类,但是有时
- 函数原型resample(self, rule, how=None, axis=0, fill_method=None, closed=No
- 代码很简单,这里就不多废话了,直接奉上:$(function(){ var w=
- 通过视图来访问数据,其优点是非常明显的。如可以起到数据保密、保证数据的逻辑独立性、简化查询操作等等。但是,话说回来,SQL Server数据
- 本文实例讲述了Python栈的实现方法。分享给大家供大家参考,具体如下:Python实现栈栈的数组实现:利用python列表方法代码如下:#
- 一、Pandoc转换1.1 问题由于我们markdown编辑器比较特殊,一般情况下,我们不太好看,如果转换成pdf的话,我们就不需要可以的去
- 一、数据无量纲化处理 (热力图)1.数据无量纲化处理(仅介绍本文用到的方法):min-max归一化该方法是对原始数据进行线性变换,
- 前不久微信上线了拍一拍功能,刚推出就被有才的网友玩坏了。还有更多没有节操的拍法这里就不展示了。但拍一拍属于弱提示,只有在聊天界面才能感受到。
- url标记为变量通过把 URL 的一部分标记为 <variable_name> 就可以在 URL 中添加变量。标记的 部分会作为
- 需要安装pyechartspip install pyecharts -U 创建【demo6.py】并输入以下编码:from py
- 今天来分享python学习的一个小例子,使用python暴力破解mysql数据库,实现方式是通过UI类库tkinter实现可视化面板效果,在
- 一、实验内容编写一Python程序,要求实现以下功能:读入一幅图像。使用两种以上的方法分别向图像中添加噪声。输出一幅二值图像,图像中未加入噪
- 动态规划(Dynamic Programming,DP)是一种常用的算法思想,通常用于解决具有重叠子问题和最优子结构性质的问题。动态规划算法
- mysq 正确清理binlog日志前言:MySQL中的binlog日志记录了数据库中数据的变动,便于对数据的基于时间点和基于位置的恢复,但是
- 如下所示:import threadingimport timesem=threading.Semaphore(4) #限制线程的最大数量为
- python 3.x 环境下,使用h5py加载HDF5文件,查看keys,如下:>>> import h5py>&g
- 一、变量相关1.变量声明C# : int a;Go : var a int; 需要在前面加一个var关键字,后面定义类型可以使用 var(