3 回答

TA貢獻(xiàn)2012條經(jīng)驗(yàn) 獲得超12個(gè)贊
使用反射你可以這樣做:
rt := reflect.ValueOf(i).Type()
for rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
// rt is non-pointer type
https://play.golang.com/p/YbT1p4R3_1u

TA貢獻(xiàn)2041條經(jīng)驗(yàn) 獲得超4個(gè)贊
使用以下間接值:
v := reflect.ValueOf(i)
for v.Kind() == reflect.Ptr && !v.IsNil() {
v = v.Elem()
}
// v.Interface() is nil pointer or non-pointer

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊
你正在尋找reflect.Indirect(interface{}),
// Indirect returns the value that v points to.
// If v is a nil pointer, Indirect returns a zero Value.
// If v is not a pointer, Indirect returns v.
rv := reflect.Indirect(v)
- 3 回答
- 0 關(guān)注
- 161 瀏覽
添加回答
舉報(bào)