在学习nsq源码时,再次重温一下 go 的 reflect 包。
variable of interface type 接口类型变量
A variable of interface type stores a pair: the concrete value assigned to the variable, and that value’s type descriptor.
接口类型变量存着两部分,具体的值和值类型描述符。
1 | var t *int = nil |
Anonymous
1 | // A StructField describes a single field in a struct. |
可以通过 field 的 Anonymous 判断该字段是否是内嵌字段
Elem vs Indirect
reflect.Value 是一个指针,那么 v.Elem() 和 reflect.Indirect(v) 等价的。reflect.Value 不是指针,那么 reflect.Indirect(v) 返回自身。reflect.Value 不是指针但是一个 interface 那么 v.Elem() 返回 interface 所包含的值reflect.Value 不是指针也不是一个 interface 那么 v.Elem() 会发生panic。
reflect.ValueOf()
1 | var x float64 = 3.4 |
1 | var x float64 = 3.4 |
1 | var x float64 = 3.4 |
反射是接口变量到反射对象
反射是反射对象到接口变量
要修改反射对象的值那么一定是要settable