2.reflect.TypeOf()

Go反射主要与interface相关,只有interface类型才有反射一说。

反射就是用来访问存储在接口变量内部类型、值的一种机制。

直接获取到变量内部的信息

Go的reflect包提供了两种方法,可以访问接口变量内容reflect.TypeOf()reflect.ValueOf()

1. reflect.TypeOf(i interface{}) Type 方法

//src\reflect\type.go

// TypeOf returns the reflection Type that represents the dynamic type of i.
// If i is a nil interface value, TypeOf returns nil.
func TypeOf(i interface{}) Type {
	eface := *(*emptyInterface)(unsafe.Pointer(&i)) 
	return toType(eface.typ)    
}

func toType(t *rtype) Type {
	if t == nil {
		return nil
	}
	return t
}
2 TypeOf()返回内容

由于TypeOf()入参为空接口。在将一个接口传递给TypeOf时,可能会发生类型转换。

  1. i不是空接口,即iface对象。会产生convI2E转换,将iface的动态类型赋值为eface动态类型。
  2. i本身就是空接口。直接赋值。
  3. eface.typ即入参的动态类型转换为Type接口类型。(接口类型为Type,值为eface.typ

3 总结

  1. TypeOf返回入一个iface变量。
  2. iface.tabreflect.Type接口类型。
  3. iface.data为入参的动态类型
  4. TypeOf不关心入参的值。
type iface struct {
	tab  *itab
	data unsafe.Pointer
}
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.
}
type interfacetype struct {
	typ     _type
	pkgpath name
	mhdr    []imethod
}
// reflect.emptyInterface == runtime.eface
// emptyInterface is the header for an interface{} value.
type emptyInterface struct {
	typ  *rtype
	word unsafe.Pointer
}

4. TypeOf实现原理

5   type person struct {
6       name string
7       age int32
8   }
23  var var_type reflect.Type = reflect.TypeOf(var_person)

对应汇编

0x00e0 00224 (reflect.go:23)    MOVL    "".var_person+1800(SP), AX
0x00e7 00231 (reflect.go:23)    MOVQ    "".var_person+1792(SP), CX
0x00ef 00239 (reflect.go:23)    PCDATA  $2, $3
0x00ef 00239 (reflect.go:23)    MOVQ    "".var_person+1784(SP), DX
0x00f7 00247 (reflect.go:23)    PCDATA  $2, $0
0x00f7 00247 (reflect.go:23)    PCDATA  $0, $4
    //临时变量autotmp_77,保存var_person
0x00f7 00247 (reflect.go:23)    MOVQ    DX, ""..autotmp_77+2024(SP) //name字符串存放地址
0x00ff 00255 (reflect.go:23)    MOVQ    CX, ""..autotmp_77+2032(SP) //name字符串长度
0x0107 00263 (reflect.go:23)    MOVL    AX, ""..autotmp_77+2040(SP) //age

    // 构造一个eface, 调用runtime.convT2E(), 准备两个参数入栈,1)动态类型。2)值。
0x010e 00270 (reflect.go:23)    PCDATA  $2, $1
0x010e 00270 (reflect.go:23)    LEAQ    type."".person(SB), AX  //将person类型放入AX
0x0115 00277 (reflect.go:23)    PCDATA  $2, $0
0x0115 00277 (reflect.go:23)    MOVQ    AX, (SP)    //person类型压入栈 **
0x0119 00281 (reflect.go:23)    PCDATA  $2, $1
0x0119 00281 (reflect.go:23)    PCDATA  $0, $3
0x0119 00281 (reflect.go:23)    LEAQ    ""..autotmp_77+2024(SP), AX //将临时变量autotmp_77,放入AX 
0x0121 00289 (reflect.go:23)    PCDATA  $2, $0
0x0121 00289 (reflect.go:23)    MOVQ    AX, 8(SP)   //将临时变量autotmp_77入栈 **
    //  调用convT2E(),构造一个eface变量
0x0126 00294 (reflect.go:23)    CALL    runtime.convT2E(SB) //将临时变量转换为eface变量。** 
0x012b 00299 (reflect.go:23)    PCDATA  $2, $1
0x012b 00299 (reflect.go:23)    MOVQ    24(SP), AX  //返回值 
0x0130 00304 (reflect.go:23)    MOVQ    16(SP), CX  //返回值
    // 临时变量autotmp_83保存eface对象
0x0135 00309 (reflect.go:23)    MOVQ    CX, ""..autotmp_83+1304(SP) //动态类型==person
0x013d 00317 (reflect.go:23)    MOVQ    AX, ""..autotmp_83+1312(SP) //变量值==var_person
0x0145 00325 (reflect.go:23)    PCDATA  $0, $5
    // 入参i interface{}即为一个eface对象,将得到的eface变量设置给入参
0x0145 00325 (reflect.go:23)    MOVQ    CX, reflect.i+888(SP)   //将临时eface变量设置到TypeOf()入参i. eface动态类型
0x014d 00333 (reflect.go:23)    PCDATA  $2, $0
0x014d 00333 (reflect.go:23)    MOVQ    AX, reflect.i+896(SP)   //将临时eface变量设置到TypeOf()入参i. eface值。
0x0155 00341 (reflect.go:23)    XORPS   X0, X0
0x0158 00344 (reflect.go:23)    MOVUPS  X0, "".~R0+680(SP)  //返回值清零
0x0160 00352 (reflect.go:23)    XCHGL   AX, AX      //写屏障
0x0161 00353 (reflect.go:23)    XORPS   X0, X0
0x0164 00356 (reflect.go:23)    MOVUPS  X0, reflect.eface·3+1144(SP)    //置空TypeOf函数中临时变量eface.
    
0x016c 00364 ($GOROOT/src/reflect/type.go:1375) PCDATA  $2, $1
0x016c 00364 ($GOROOT/src/reflect/type.go:1375) LEAQ    reflect.i+888(SP), AX   // 动态类型 struct person放入AX
0x0174 00372 ($GOROOT/src/reflect/type.go:1375) PCDATA  $2, $0
0x0174 00372 ($GOROOT/src/reflect/type.go:1375) TESTB   AL, (AX)        //确保不是nil接口
0x0176 00374 ($GOROOT/src/reflect/type.go:1375) PCDATA  $2, $1
0x0176 00374 ($GOROOT/src/reflect/type.go:1375) MOVQ    reflect.i+896(SP), AX   // 变量值 var_person到 AX
0x017e 00382 ($GOROOT/src/reflect/type.go:1375) PCDATA  $2, $4
0x017e 00382 ($GOROOT/src/reflect/type.go:1375) PCDATA  $0, $3
0x017e 00382 ($GOROOT/src/reflect/type.go:1375) MOVQ    reflect.i+888(SP), CX   // 动态类型struct person 到CX
    //设置临时变量eface
0x0186 00390 ($GOROOT/src/reflect/type.go:1375) MOVQ    CX, reflect.eface·3+1144(SP)    //设置临时变量eface的动态类型
0x018e 00398 ($GOROOT/src/reflect/type.go:1375) PCDATA  $2, $2
0x018e 00398 ($GOROOT/src/reflect/type.go:1375) MOVQ    AX, reflect.eface·3+1152(SP)    //设置临时变量的值

    // 调用toType,将eface.typ,即动态类型转换为Type接口。1376 toType(eface.typ)
0x0196 00406 ($GOROOT/src/reflect/type.go:1376) PCDATA  $2, $0
0x0196 00406 ($GOROOT/src/reflect/type.go:1376) PCDATA  $0, $6
0x0196 00406 ($GOROOT/src/reflect/type.go:1376) MOVQ    CX, reflect.t+216(SP)   // 将动态类型,即struct person设置给临时变量t
0x019e 00414 ($GOROOT/src/reflect/type.go:1376) XORPS   X0, X0
0x01a1 00417 ($GOROOT/src/reflect/type.go:1376) MOVUPS  X0, "".~R0+824(SP)  //清零返回值
0x01a9 00425 ($GOROOT/src/reflect/type.go:1376) XCHGL   AX, AX  //写屏障
    // func toType(t *rtype) Type 
0x01aa 00426 ($GOROOT/src/reflect/type.go:3004) CMPQ    reflect.t+216(SP), $0   // 判断是否nil类型
0x01b3 00435 (:0)       JNE     442
0x01b5 00437 (:0)       JMP     6716
0x01ba 00442 ($GOROOT/src/reflect/type.go:3007) PCDATA  $2, $1
0x01ba 00442 ($GOROOT/src/reflect/type.go:3007) PCDATA  $0, $3
0x01ba 00442 ($GOROOT/src/reflect/type.go:3007) MOVQ    reflect.t+216(SP), AX   // 将动态类型即struct person设置到AX
0x01c2 00450 ($GOROOT/src/reflect/type.go:3007) MOVQ    AX, ""..autotmp_78+280(SP)  //动态类型赋值给临时变量 autotmp_78,没啥用
0x01ca 00458 ($GOROOT/src/reflect/type.go:1376) PCDATA  $2, $4

    //重点逻辑
    // 临时变量autotmp_84,类型是Type接口,即iface变量。  
0x01ca 00458 ($GOROOT/src/reflect/type.go:1376) LEAQ    go.itab.*reflect.rtype,reflect.Type(SB), CX
    //设置iface.tab为go.itab.*reflect.rtype
0x01d1 00465 ($GOROOT/src/reflect/type.go:1376) MOVQ    CX, ""..autotmp_84+1288(SP) 
    //有0x01ba知道AX中放的是struct person的类型,
    //设置iface.data为入参的动态类型部分。 
0x01d9 00473 ($GOROOT/src/reflect/type.go:1376) MOVQ    AX, ""..autotmp_84+1296(SP) // 设置返回值接口变量值部分设置为动态类型 


0x01e1 00481 ($GOROOT/src/reflect/type.go:1376) PCDATA  $2, $1
0x01e1 00481 ($GOROOT/src/reflect/type.go:1376) PCDATA  $0, $7
0x01e1 00481 ($GOROOT/src/reflect/type.go:1376) MOVQ    CX, "".~R0+824(SP)  // 接口类型Type
0x01e9 00489 ($GOROOT/src/reflect/type.go:1376) PCDATA  $2, $0
0x01e9 00489 ($GOROOT/src/reflect/type.go:1376) MOVQ    AX, "".~R0+832(SP)  // 接口值即动态类型
0x01f1 00497 ($GOROOT/src/reflect/type.go:1376) JMP     499
0x01f3 00499 (reflect.go:23)    PCDATA  $2, $1
0x01f3 00499 (reflect.go:23)    MOVQ    "".~R0+832(SP), AX
0x01fb 00507 (reflect.go:23)    PCDATA  $0, $3
0x01fb 00507 (reflect.go:23)    MOVQ    "".~R0+824(SP), CX
0x0203 00515 (reflect.go:23)    MOVQ    CX, ""..autotmp_85+1272(SP)
0x020b 00523 (reflect.go:23)    MOVQ    AX, ""..autotmp_85+1280(SP)
0x0213 00531 (reflect.go:23)    MOVQ    CX, "".~R0+680(SP)
0x021b 00539 (reflect.go:23)    MOVQ    AX, "".~R0+688(SP)
0x0223 00547 (reflect.go:23)    JMP     549
0x0225 00549 (reflect.go:23)    PCDATA  $0, $8
0x0225 00549 (reflect.go:23)    MOVQ    CX, "".var_type+840(SP)
0x022d 00557 (reflect.go:23)    PCDATA  $2, $0
0x022d 00557 (reflect.go:23)    MOVQ    AX, "".var_type+848(SP)
type."".person
type."".person SRODATA size=160
        0x0000 18 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00  ................
        0x0010 0a 75 d3 7f 07 08 08 19 00 00 00 00 00 00 00 00  .u..............
        0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
        0x0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
        0x0040 02 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00  ................
        0x0050 00 00 00 00 01 00 01 00 40 00 00 00 00 00 00 00  ........@.......
        0x0060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
        0x0070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
        0x0080 00 00 00 00 00 00 00 00 20 00 00 00 00 00 00 00  ........ .......
        0x0090 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
        rel 24+8 t=1 type..alg."".person+0
        rel 32+8 t=1 runtime.gcbits.01+0
        rel 40+4 t=5 type..namedata.*main.person-+0
        rel 44+4 t=5 type.*"".person+0
        rel 48+8 t=1 type..importpath."".+0
        rel 56+8 t=1 type."".person+96
        rel 80+4 t=5 type..importpath."".+0
        rel 96+8 t=1 type..namedata.name-+0
        rel 104+8 t=1 type.string+0
        rel 120+8 t=1 type..namedata.age-+0
        rel 128+8 t=1 type.int32+0
        rel 144+4 t=5 type..namedata.Test_MethodByName1.+0
        rel 148+4 t=24 type.func()+0
        rel 152+4 t=24 "".(*person).Test_MethodByName1+0
        rel 156+4 t=24 "".person.Test_MethodByName1+0
5 reflect.rtype== runtime._type
// rtype must be kept in sync with ../runtime/type.go:/^type._type.
type rtype struct {     //共48字节
	size       uintptr  // 8 字节
	ptrdata    uintptr  // 8 字节 number of bytes in the type that can contain pointers
	hash       uint32   // 4 字节 hash of type; avoids computation in hash tables
	tflag      tflag    // 1 字节 extra type information flags
	align      uint8    // 1 字节 alignment of variable with this type
	fieldAlign uint8    // 1 字节 alignment of struct field with this type
	kind       uint8    // 1 字节 enumeration for C
	alg        *typeAlg // 8 字节 algorithm table
	gcdata     *byte    // 8 字节 garbage collection data
	str        nameOff  // 4 字节 string form
	ptrToThis  typeOff  // 4 字节 type for pointer to this type, may be zero
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值