lundi 28 décembre 2020

how to create a pointer to reflect.Value

I see many questions that appear to ask what I'm asking but I didn't see any actual response to the general question, just specific stuff.

I have reflect.Value of an int with a value of 64.

I have a slice that I got with reflection, that the type of the items in the slice is *int.

newSlice := reflect.Append(slice, currentRow.Convert(slice.Type().Elem()))
slice.Set(newSlice)

if I just run this, I get panic: reflect.Value.Convert: value of type int cannot be converted to type *int

I tried to run currentRow.Interface() but it returns an interface type pointer that cannot be added to the slice.

so how can I do that properly ?

I cannot know if it will be *int, or *string or anything else, I need it to be genneric





Aucun commentaire:

Enregistrer un commentaire