type eface struct { _type *_type data unsafe.Pointer }
type _type struct { size uintptr// type size ptrdata uintptr// size of memory prefix holding all pointers hash uint32// hash of type; avoids computation in hash tables tflag tflag // extra type information flags align uint8// alignment of variable with this type fieldalign uint8// alignment of struct field with this type kind uint8// enumeration for C alg *typeAlg // algorithm table gcdata *byte// garbage collection data str nameOff // string form ptrToThis typeOff // type for pointer to this type, may be zero }
type iface struct { tab *itab data unsafe.Pointer }
// 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 link *itab bad int32 inhash int32// has this itab been added to hash? fun [1]uintptr// variable sized }
代码 17 行 var y interface{} = x 调用了函数 runtime.convT2E ,将 int 类型的 x 转换成 empty interface。代码 19 行 var t MyInterface = s 将 MyStruct 类型转换成 non-empty interface: MyInterface。
type itab struct { inter *interfacetype _type *_type link *itab bad int32 inhash int32// has this itab been added to hash? fun [1]uintptr// variable sized }
var ( ifaceLock mutex // lock for accessing hash hash [hashSize]*itab )
func itabhash(inter *interfacetype, typ *_type) uint32 { // compiler has provided some good hash codes for us. h := inter.typ.hash h += 17 * typ.hash // TODO(rsc): h += 23 * x.mhash ? return h % hashSize }
type eface struct { _type *_type data unsafe.Pointer }
type _type struct { size uintptr// type size ptrdata uintptr// size of memory prefix holding all pointers hash uint32// hash of type; avoids computation in hash tables tflag tflag // extra type information flags align uint8// alignment of variable with this type fieldalign uint8// alignment of struct field with this type kind uint8// enumeration for C alg *typeAlg // algorithm table gcdata *byte// garbage collection data str nameOff // string form ptrToThis typeOff // type for pointer to this type, may be zero }
type iface struct { tab *itab data unsafe.Pointer }
// 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 link *itab bad int32 inhash int32// has this itab been added to hash? fun [1]uintptr// variable sized }
代码 17 行 var y interface{} = x 调用了函数 runtime.convT2E ,将 int 类型的 x 转换成 empty interface。代码 19 行 var t MyInterface = s 将 MyStruct 类型转换成 non-empty interface: MyInterface。
type itab struct { inter *interfacetype _type *_type link *itab bad int32 inhash int32// has this itab been added to hash? fun [1]uintptr// variable sized }
var ( ifaceLock mutex // lock for accessing hash hash [hashSize]*itab )
func itabhash(inter *interfacetype, typ *_type) uint32 { // compiler has provided some good hash codes for us. h := inter.typ.hash h += 17 * typ.hash // TODO(rsc): h += 23 * x.mhash ? return h % hashSize }