struct
go接口有两种定义:eface
和iface
。 type eface struct {
_type *_type
data unsafe.Pointer
}
type iface struct {
tab *itab
data unsafe.Pointer
}
eface
表示没有任何方法的接口:interface{}
.有eface
两个字段:_type
和data
。data
实际上是一个指向真实数据的指针。_type
定义如下: type _type struct {
size uintptr
ptrdata uintptr
hash uint32
tflag tflag
align uint8
fieldalign uint8
kind uint8
alg *typeAlg
gcdata *btye
str nameOff
ptrToThis typeOff
}
_type
是接口内部存储的具体数据的真实类型。iface
两个字段:tab
和data
。data
是指向真实数据的指针。itab
定义如下: type itab struct {
inter *interfacetype
_type *_type
hash uint32
_ [4]byte
fun [1]uintptr
}
type interfacetype struct {
typ _type
pkgpath name
mhdr []imethod
}
interfacetype
描述接口自己的类型。是具体数据的_type
类型,同_type
in eface
。fun
是指向方法集的指针。它用于动态调度。reflect.TypeOf()
时,它将被转换为interface{}
类型。如果传递的值是接口类型,具体的数据类型将存储在eface
_type
字段中。然后,将eface
struct 转换emptyInterface
为 have typ
and word
。 func TypeOf(i interface{}) Type {
eface := *(emptyInterface)(unsafe.Poiter(&i))
return toType(eface.typ)
}
type emptyInterface struct {
typ *rtype
word unsafe.Pointer
}
reflect.Value
是一个结构体,定义如下: // Value is the reflection interface to a Go value.
//
// Not all methods apply to all kinds of values. Restrictions,
// if any, are noted in the documentation for each method.
// Use the Kind method to find out the kind of value before
// calling kind-specific methods. Calling a method
// inappropriate to the kind of type causes a run time panic.
//
// The zero Value represents no value.
// Its IsValid method returns false, its Kind method returns Invalid,
// its String method returns "<invalid Value>", and all other methods panic.
// Most functions and methods never return an invalid value.
// If one does, its documentation states the conditions explicitly.
//
// A Value can be used concurrently by multiple goroutines provided that
// the underlying Go value can be used concurrently for the equivalent
// direct operations.
//
// To compare two Values, compare the results of the Interface method.
// Using == on two Values does not compare the underlying values
// they represent.
type Value struct {
// typ holds the type of the value represented by a Value.
typ *rtype
// Pointer-valued data or, if flagIndir is set, pointer to data.
// Valid when either flagIndir is set or typ.pointers() is true.
ptr unsafe.Pointer
// flag holds metadata about the value.
// The lowest bits are flag bits:
// - flagStickyRO: obtained via unexported not embedded field, so read-only
// - flagEmbedRO: obtained via unexported embedded field, so read-only
// - flagIndir: val holds a pointer to the data
// - flagAddr: v.CanAddr is true (implies flagIndir)
// Value cannot represent method values.
// The next five bits give the Kind of the value.
// This repeats typ.Kind() except for method values.
// The remaining 23+ bits give a method number for method values.
// If flag.kind() != Func, code can assume that flagMethod is unset.
// If ifaceIndir(typ), code can assume that flagIndir is set.
flag
// A method value represents a curried method invocation
// like r.Read for some receiver r. The typ+val+flag bits describe
// the receiver r, but the flag's Kind bits say Func (methods are
// functions), and the top bits of the flag give the method number
// in r's type's method table.
}
func ValueOf(i interface{}) Value {
if i == nil {
return Value{}
}
escapes(i)
return unpackEface(i)
}
func unpackEface(i interface{}) Value {
e := (*emptyInterface)(unsafe.Pointer(&i))
t := e.typ
if t == nil {
return Value{}
}
f := flag(t.Kind())
if ifaceIndir(t) {
f |= flagIndir
}
return Value{t, e.word, f}
}
Clash Verge v1.5.7 更新ChatGPT 领域创新应用:小 K、小鱼及小冰,引领智能在线服务新时代网球对决游戏中“包位已满”问题解决攻略如何安装Clash并解决上不了网的问题Midjourney直接生成UI界面! AI设计体验指南如何使用 TAG VPN 机场翻墙?Clash更新之后怎么配置Android v2ray全面指南国内谷歌云:概念、特点、优势、应用与常见问题详解国内v2ray网速慢问题解决指南如何使用 Nexitally 奶昔机场翻墙?2025 新版苹果 Mac 电脑 ClashX Pro 下载和使用教程Clash Meta for Android最新Clash安卓客户端下载 机场节点订阅配置使用教程GUI.for.SingBox Windows 版快速上手教程:轻松管理 SingBox 配置,附带最新下载地址MESL 机场怎么样 – 优质 SS 机场推荐 | BGP 入口 IPLC 专线机场