3. reflect.ChanDir()

1. 接口方法实现-ChanDir() 方法

reflect.Type变量是一个接口iface,有两部分。

  1. 接口类型,即reflect.Type
  2. 接口值,即动态类型

调用ChanDir()方法过程:

  1. 将Type接口的值,即动态类型入栈。
  2. 调用reflect.(*rtype).ChanDir()方法。即动态类型的接口方法ChanDir()
  3. 判断动态类型是否chan,否则panic。
  4. 将动态类型转换为chanType类型。
  5. 返回chanType类型中的chan方向。
// chanType represents a channel type.
type chanType struct {
	rtype
	elem *rtype  // channel element type
	dir  uintptr // channel direction (ChanDir)
}

方法调用示例

// var var_chan chan(int)
// var var_reflect_chan reflect.Type = reflect.TypeOf(var_chan)

0x03d1 00977 ($GOROOT/src/reflect/type.go:3007) MOVQ    reflect.t+256(SP), AX   //func toType(t *rtype) Type , t即chan(int)类型
0x03e1 00993 ($GOROOT/src/reflect/type.go:1376) LEAQ    go.itab.*reflect.rtype,reflect.Type(SB), CX
0x043c 01084 (reflect.go:31)    MOVQ    CX, "".var_reflect_chan+968(SP) // CX是接口类型,即reflect.Type类型
0x0444 01092 (reflect.go:31)    PCDATA  $2, $0
0x0444 01092 (reflect.go:31)    MOVQ    AX, "".var_reflect_chan+976(SP) // AX是动态类型,chan(int) 类型,是接口值
var_reflect_chan.ChanDir();
// var_reflect_chan.ChanDir();

0x0454 01108 (reflect.go:33)    MOVQ    "".var_reflect_chan+968(SP), AX // reflect.Type类型
0x045c 01116 (reflect.go:33)    TESTB   AL, (AX)
0x045e 01118 (reflect.go:33)    MOVQ    48(AX), AX  //Type接口变量ChanDir函数偏移位置48
0x0462 01122 (reflect.go:33)    PCDATA  $2, $2
0x0462 01122 (reflect.go:33)    MOVQ    "".var_reflect_chan+976(SP), CX
0x046a 01130 (reflect.go:33)    PCDATA  $2, $0
0x046a 01130 (reflect.go:33)    MOVQ    CX, (SP)    //将类型 chan(int)入栈
0x046e 01134 (reflect.go:33)    CALL    AX  //调用运行时函数  ChanDir()
go.itab.*reflect.rtype,reflect.Type
//
go.itab.*reflect.rtype,reflect.Type SRODATA dupok size=272
0x0000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0x0010 d6 c9 33 e3 00 00 00 00 00 00 00 00 00 00 00 00  ..3.............
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 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0x0050 00 00 00 00 00 00 00 00 00 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 00 00 00 00 00 00 00 00  ................
0x0090 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0x00a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0x00b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0x00c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0x00d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0x00e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0x00f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0x0100 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
rel 0+8 t=1 type.reflect.Type+0
rel 8+8 t=1 type.*reflect.rtype+0
rel 24+8 t=1 reflect.(*rtype).Align+0
rel 32+8 t=1 reflect.(*rtype).AssignableTo+0
rel 40+8 t=1 reflect.(*rtype).Bits+0
rel 48+8 t=1 reflect.(*rtype).ChanDir+0
...
2. ChanDir()实现

src\reflect\type.go

func (t *rtype) ChanDir() ChanDir {
	if t.Kind() != Chan {
		panic("reflect: ChanDir of non-chan type")
	}
	tt := (*chanType)(unsafe.Pointer(t))
	return ChanDir(tt.dir)
}
2.1. 类型判断
  1. rtype/_type有kind属性。uint8类型共32个字节。 kind uint8 // enumeration for C
  2. 实际只用了31位来描述类型,kindMask=1<<5-1=31.
  3. Kind种类个数总共27种。
  4. Kind()方法就是获取kind属性。
  5. 类型不匹配时,就panic。
type Kind uint
const (
	kindDirectIface = 1 << 5
	kindGCProg      = 1 << 6 // Type.gc points to GC program
	kindNoPointers  = 1 << 7
	kindMask        = (1 << 5) - 1
)

func (t *rtype) Kind() Kind { return Kind(t.kind & kindMask) }


const (
	Invalid Kind = iota
	Bool
	Int
	Int8
	Int16
	Int32
	Int64
	Uint
	Uint8
	Uint16
	Uint32
	Uint64
	Uintptr
	Float32
	Float64
	Complex64
	Complex128
	Array
	Chan
	Func
	Interface
	Map
	Ptr
	Slice
	String
	Struct
	UnsafePointer
)
2.2. 将类型强制转换为目标类型。
	tt := (*chanType)(unsafe.Pointer(t))
2.3. 调用目标类型对应的方法、属性。
ChanDir(tt.dir)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值