_typeuncommontypeefaceiface
reflect.Typereflect.Value
Type
reflect.Type
type Type interface {
Align() int //对齐边界
FieldAlign() int //作为结构体字段的对齐边界
Method(int) Method //获取方法数组中第i个Method
MethodByName(string) (Method, bool) //按照名称查找方法
NumMethod() int //方法列表中可导出方法的数目
Name() string //类型名称
PkgPath() string //包路径
Size() uintptr //该类型变量占用字节数
String() string //获取类型的字符串表示
Kind() Kind //类型对应的reflect.Kind
Implements(u Type) bool //该类型是否实现了接口u
AssignableTo(u Type) bool //是否可以赋值给类型u
ConvertibleTo(u Type) bool //是否可转换为类型u
Comparable() bool //是否可比较
//只能应用于某些Kind的方法
//Int*, Uint*, Float*, Complex*:
Bits() int
//Array,Ptr,Slice,Map:
Elem() Type
//Array
Len() int
//Chan:ChanDir, Elem
ChanDir() ChanDir
//Func:
In(i int) Type
NumIn() int
Out(i int) Type
NumOut() int
IsVariadic() bool
//Map:
Key() Type
//Struct:
Field(i int) StructField
FieldByIndex(index []int) StructField
FieldByName(name string) (StructField, bool)
FieldByNameFunc(match func(string) bool) (StructField, bool)
NumField() int
common() *rtype
uncommon() *uncommonType
}
reflect.TypeOfreflect.Type
func TypeOf(i interface{}) Type {
eface := *(*emptyInterface)(unsafe.Pointer(&i))
return toType(eface.typ)
}
reflect.TypeOfruntime.efaceieface
*rtypereflect.Typeefacetypreflect.Typeeface.typreflect.Type
var ret reflect.Type
ret = eface.typ
eface.typdataeface.typstringitabreflect.Type*rtype
*rtypedataImplements
type Stringer interface {
String() string
}
reflectTypeTypeOf
Value
reflect.Typereflect.Value
type Value struct {
typ *rtype
ptr unsafe.Pointer
flag
}
typptrflag
reflect.Value
func main() {
a := "eggo"
v := reflect.ValueOf(a)
v.SetString("new eggo")
println(a)
}
reflect.ValueOfTypeOfa
func ValueOf(i interface{}) Value {
if i == nil {
return Value{}
}
escapes(i)
return unpackEface(i)
}
reflect.ValueOfescapesicopy of aicopy of a
ValueOfreflect.Valuetyp_typestringptrdatacopy of a
vv.SetStringvv.ptrcopy of aa
copy of a
panic: reflect: reflect.Value.SetString using unaddressable value
aa
func main() {
a := "eggo"
v := reflect.ValueOf(&a)
v.Elem().SetString("new eggo")
println(a)
}
areflect.ValueOfa
vv.typ*stringv.ptra
v.Elem()v.ptrareflect.Valuetypstringptra
v.Elem()SetStringa
Value
参考资料: