如何將 reflect.Value 轉(zhuǎn)換為它的類型?type Cat struct { Age int}cat := reflect.ValueOf(obj)fmt.Println(cat.Type()) // Catfmt.Println(Cat(cat).Age) // doesn't compilefmt.Println((cat.(Cat)).Age) // same
3 回答

皈依舞
TA貢獻(xiàn)1851條經(jīng)驗 獲得超3個贊
concreteCat,_ := reflect.ValueOf(cat).Interface().(Cat)
參見http://golang.org/doc/articles/laws_of_reflection.html 狐貍示例
type MyInt int
var x MyInt = 7
v := reflect.ValueOf(x)
y := v.Interface().(float64) // y will have type float64.
fmt.Println(y)

慕桂英3389331
TA貢獻(xiàn)2036條經(jīng)驗 獲得超8個贊
好的,我找到了
reflect.Value
有一個函數(shù)Interface()
可以將其轉(zhuǎn)換為interface{}
- 3 回答
- 0 關(guān)注
- 329 瀏覽
添加回答
舉報
0/150
提交
取消