要獲得任何切片的長(zhǎng)度,我使用reflect.ValueOf(slice).Len(). 要設(shè)置任何切片的長(zhǎng)度,我使用reflect.ValueOf(&slice).Elem().SetLen(n).我的結(jié)構(gòu)中有一個(gè)類(lèi)型的字段reflect.Value,并且該值設(shè)置為,reflect.ValueOf(&slice)以便我可以更改切片。但現(xiàn)在我無(wú)法獲得底層切片的長(zhǎng)度。call of reflect.Value.Len on ptr Value它會(huì)因?yàn)槿绻襆en()直接打電話而感到恐慌,如果我打電話call of reflect.Value.Len on interface Value給Elem().Len().以下是我試圖實(shí)現(xiàn)的功能:func pop(slice interface{}) interface{} { v := reflect.ValueOf(slice) length := v.Len() last := v.Index(length - 1) v.SetLen(length - 1) return last}我怎樣才能同時(shí)使用refect.Value切片指針?
獲取和設(shè)置任意切片的長(zhǎng)度
達(dá)令說(shuō)
2022-05-23 15:49:42