interface
底层结构
#
interface{}
底层存储结构如下所示:
// ----------- runtime/runtime2.go -----------------
type iface struct {
tab *itab
data unsafe.Pointer
}
type eface struct {
_type *_type
data unsafe.Pointer
}
type itab = abi.ITab
type _type = abi.Type
// ----------- internal/abi/iface.go -----------------
// The first word of every non-empty interface type contains an *ITab.
// It records the underlying concrete type (Type), the interface type it
// is implementing (Inter), and some ancillary information.
//
// allocated in non-garbage-collected memory
type ITab struct {
Inter *InterfaceType
Type *Type
Hash uint32 // copy of Type.Hash. Used for type switches.
Fun [1]uintptr // variable sized. fun[0]==0 means Type does not implement Inter.
}
可以看到 interface{}
底层被存储为两种类型: