reflection - Set slice index using reflect in Go -


i'm in go, working reflect.value representation of slice. have following:

slice := reflect.makeslice(typ, len, cap) 

if want ith value slice, it's simple:

v := slice.index(i) // returns reflect.value 

however, can't seem find way set ith value. reflect.value has lots of setter methods, example, if had map, m, following possible:

m.setmapindex(key, value) // key , value have type reflect.value 

but there doesn't seem equivalent slices. 1 thought maybe value returned slice.index(i) pointer somehow, calling v := slice.index(i); v.set(newv) work? i'm not sure. ideas?

figured out! turns out posted prematurely - guess slice.index(0) returns pointer correct. in particular:

one := reflect.valueof(int(1))  slice := reflect.makeslice(reflect.typeof([]int{}), 1, 1) v := slice.index(0) fmt.println(v.interface())  v.set(one) fmt.println(v.interface())  v = slice.index(0) fmt.println(v.interface()) 

prints:

0 1 1 

(here's runnable code on go playground)


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -