Go源码: interface

发布于 2019-06-04 · 本文总共 1288 字 · 阅读大约需要 4 分钟

interface

源码分析

eface

/usr/local/go/src/runtime/runtime2.go

不含method的interface结构,或者叫empty interface

type eface struct {
	_type *_type
	data  unsafe.Pointer
}

_type

// Needs to be in sync with ../cmd/link/internal/ld/decodesym.go:/^func.commonsize,
// ../cmd/compile/internal/gc/reflect.go:/^func.dcommontype and
// ../reflect/type.go:/^type.rtype.
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
}

iface

/usr/local/go/src/runtime/runtime2.go

iface表示non-empty interface的底层实现。 相比于empty interface,non-empty要包含一些method。 method的具体实现存放在itab.fun变量里

iface

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

itab

// layout of Itab known to compilers
// allocated in non-garbage-collected memory
// Needs to be in sync with
// ../cmd/compile/internal/gc/reflect.go:/^func.dumptypestructs.
type itab struct {
	inter *interfacetype
	_type *_type
	hash  uint32 // copy of _type.hash. Used for type switches.
	_     [4]byte
	fun   [1]uintptr // variable sized. fun[0]==0 means _type does not implement inter.
}

$ go build -gcflags ‘-l’ -o xxxx xxx.go $ go tool objdump -s “main.main” xxxxx

go版本

go version go1.13.6 darwin/amd64




本博客所有文章采用的授权方式为 自由转载-非商用-非衍生-保持署名 ,转载请务必注明出处,谢谢。
声明:
本博客欢迎转发,但请保留原作者信息!
博客地址:邱文奇(qiuwenqi)的博客;
内容系本人学习、研究和总结,如有雷同,实属荣幸!
阅读次数:

文章评论

comments powered by Disqus


章节列表