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