type A interface {
Name() string
}
type B struct {
Age int
}
func (b B) Name() string {
return ""
}
func main() {
b := B{}
var a A = &b
fmt.Println("a->type:" , reflect.ValueOf(a).Type())
fmt.Println("a->value", reflect.ValueOf(a))
c := &a
fmt.Println("&a", &c)
fmt.Println("&a->type", reflect.ValueOf(&a).Type())
fmt.Println("&a->value", reflect.ValueOf(&a))
fmt.Println("&a->elem->type", reflect.ValueOf(&a).Elem().Type())
fmt.Println("&a->elem->value",reflect.ValueOf(&a).Elem())
fmt.Println("&a->elem->elem->type",reflect.ValueOf(&a).Elem().Elem().Type())
fmt.Println("&a->elem->elem->value",reflect.ValueOf(&a).Elem().Elem().Elem())
}
打印结果
a->type: *main.B
a->value &{0}
&a 0xc000006030
&a->type *main.A
&a->value 0xc00003a1f0(此处地址是谁的地址呢?)
&a->elem->type main.A
&a->elem->value 0xc00000a0b0 (此处地址是谁的地址呢?)
&a->elem->elem->type *main.B
&a->elem->elem->value {0}
通过反射reflect解析一个指针变量,当调用第一个value.elem()获取得到的内存地址是谁的地址呢?
有资料说对于指针对象反射,第一个elem()相当于*
号调用
那么我获取返回的内存地址 0xc00003a1f0
是哪个变量地址还是说值的开始地址
b := B{} //假设b变量的地址是x1
var a A = &b //此时a是一个指针变量,假设a变量本身的地址是x2,a变量的值是x1,即内存地址x2处保存的是x1,而x1是b变量的地址。
reflect.ValueOf(&a) //首先&a就是a变量本身的地址x2,所以reflect.ValueOf(&a)的值就是x2,在你这里面就是和c的值相等,不信你打印fmt.Println(c)是和reflect.ValueOf(&a)相等