Ezreal 书架 Ezreal 书架
Home
  • 《Go程序员面试笔试宝典》
  • 《RabbitMQ 实战指南》
  • 《深入理解kafka》
  • MySQL45讲
  • 透视HTTP协议
  • 结构化数据的分布式存储系统
  • Raft 共识算法
Home
  • 《Go程序员面试笔试宝典》
  • 《RabbitMQ 实战指南》
  • 《深入理解kafka》
  • MySQL45讲
  • 透视HTTP协议
  • 结构化数据的分布式存储系统
  • Raft 共识算法
  • 逃逸分析

  • 延迟语句

  • 数据容器

  • 通道

  • 接口

    • Go 接口与 C++接口有何异同
    • Go 语言与“鸭子类型”的关系
    • iface 和 eface 的区别是什么
    • 值接收者和指针接收者的区别
    • 如何用 interface 实现多态
    • 接口的动态类型和动态值是什么
    • 接口转换的原理是什么
    • 类型转换和断言的区别是什么
    • 如何让编译器自动检测类型是否实现了接口
  • unsafe

  • context

  • Go程序员面试笔试宝典
  • 接口
ezreal_rao
2023-05-09

iface 和 eface 的区别是什么

类型 iface 和 eface 都是 Go 中描述接口的底层结构体,区别在于 iface 描述的接口包含方法,而 eface 则是不包含任何方法的空接口:interface {}。

从源码层面看:

// src/runtime/runtime2.g

type iface struct {
  tab *itab 
  data unsafe.Pointer
}

type itab struct {
  inter *interfacetype // 接口的
  _type *_type  // 实体的类型
  link *itab
  hash uint32 // copy of _type.hash. Used for type switches.
  _ [4]byte
  fun [1]uintptr // variable sized
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

结构体 iface 内部维护两个指针,字段 tab 指向一个 itab 实体,它表示接口的类型以及赋给这个接口的实体类型;字段 data 则指向接口具体的值,一般是一个指向堆内存的指针。

再来仔细看一下 itab 结构体:_type 字段描述了实体的类型,包括内存对齐方式、大小等; inter 字段则描述了接口的类型;fun 字段放置和接口方法对应的具体数据类型的方法地址,实现接口调用方法的动态分派,一般在每次给接口赋值发生转换时会更新此表。

这里只会列出实体类型和接口相关的方法,实体类型的其他方法并不会出现在这里,可以类比 C++ 中虚函数的做法。

另外, 读者可能会觉得奇怪, 为什么 fun 数组的大小为 1, 要是接口定义了多个方法怎么办?实际上,这里存储的是第一个方法的函数指针,如果有更多的方法,会在它之后的内存空间里继续存储。从汇编角度来看,通过增加地址值就能获取到这些函数指针,没什么影响。另外,这些方法是按照函数名称的字典序进行排列的。

再看一下 interfacetype 类型,它描述的是接口的类型:

// src/runtime/type.go

type interfacetype struct {
  typ _type
  pkgpath name
  mhdr []imethod
}
1
2
3
4
5
6
7

可以看到,它包装了_type 类型,_type 实际上是描述 Go 语言中各种数据类型的结构体。注意 到,这里还包含一个 mhdr 字段,表示接口所定义的函数列表,pkgpath 记录定义了接口的包名。

如图 5-1 为 iface 结构体的全貌:

iface 结构体

接着来看 eface 的源码:

// src/runtime/runtime2.go

type eface struct {
  _type *_type
  data unsafe.Pointer
}
1
2
3
4
5
6

相比 iface,eface 就比较简单了,它只维护了一个 *_type 字段,表示空接口所承载地具体的实 体类型。data 描述了具体的值,如图 5-2 所示。

eface 结构体

来看个例子:

package main

import "fmt"

func main() {
	x := 200
	var any interface{} = x
	fmt.Println(any)

	g := Gopher{}
	var c coder = g
	fmt.Println(c)
}

type coder interface {
	code()
	debug()
}

type Gopher struct {
	language string
}

func (p Gopher) code() {
	fmt.Printf("I an coding %s language\n", p.language)
}

func (p Gopher) debug() {
	fmt.Printf("I am debuging %s language\n", p.language)
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

执行如下命令,打印出汇编语言:

go tool compile -S ./src/main.go
1

可以看到,main 函数里调用了两个函数:

func convT2E64(t *_type, elem unsafe.Pointer) (e eface)
func convT2I(tab *itab, elem unsafe.Pointer) (i iface)
1
2

上面两个函数的参数和 iface 及 eface 结构体的字段是可以联系起来的:两个函数都是将参数组装一下,形成最终的接口类型。

作为补充,最后再来看下 _type 结构体:

// src/runtime/type.go

type _type struct {
  // 类型大小
  size uintptr
  ptrdata uintptr
  // 类型的 hash 值
  hash uint32
  // 类型的 flag,和反射相关
  tflag tflag
  // 内存对齐相关
  align uint8
  fieldalign uint8
  // 类型的编号,有 bool, slice, struct 等
  kind uint8
  equal func(unsafe.Pointer, unsafe.Pointer) bool
  // GC 相关
  gcdata *byte
  str nameOff 
  ptrToThis typeOff
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Go 语言各种数据类型都是在 _type 字段的基础上,增加一些额外的字段来进行管理的:

// src/reflect/type.go

type arraytype struct {
  typ _type
  elem *_type
  slice *_type
  len uintptr
}

type chantype struct {
  typ _type
  elem *_type
  dir uintptr
}

type slicetype struct {
  typ _type
  elem *_type
}

type structtype struct {
  typ _type
  pkgPath name
  fields []structfield
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

这些数据类型的结构体定义,是反射实现的基础。

#golang#interface
上次更新: 7/12/2024, 2:37:05 PM
Go 语言与“鸭子类型”的关系
值接收者和指针接收者的区别

← Go 语言与“鸭子类型”的关系 值接收者和指针接收者的区别→

最近更新
01
为什么我的MySQL会抖一下
07-15
02
HTTP 性能优化面面观
07-12
03
WebSocket:沙盒里的 TCP
07-12
更多文章>
Theme by Vdoing | Copyright © 2022-2024 Ezreal Rao | CC BY-NC-SA 4.0
豫ICP备2023001810号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式