我有一些不同的結構像Big
與Small
嵌入在偏移0 如何訪問Small
的結構域從代碼,不知道Big
類型什麼,但是已知Small
在偏移量0?Golang:轉換結構嵌入在偏移0結構
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
類型已Small
型嵌入式偏移量爲0。有沒有辦法做這樣的事情(可能與unsafe.Pointer
)?
忽略 - 沒有真正的問題妥善 – Sridhar