我有一些不同的结构体,比如 Big
结构体在偏移量为 0 处嵌入了 Small
结构体。
如果代码不知道 Big
类型,但知道 Small
在偏移量为零处,如何访问 Small
的结构字段?
type Small struct {
val int
}
type Big struct {
Small
bigval int
}
var v interface{} = Big{}
// here i only know about 'Small' struct and i know that it is at the begining of variable
v.(Small).val // compile error
看起来编译器从理论上可以操作这样的表达式,因为它知道Big
类型在偏移量0处嵌入了Small
类型。 是否有任何方法可以做到这一点(也许使用unsafe.Pointer
)?