The following works fine:
type MyStruct struct {
MyField int32
}
func SetReflectConcrete(obj *MyStruct, fieldName string, newValue interface{}) {
objElem := reflect.ValueOf(obj).Elem()
field := objElem.FieldByName(fieldName)
field.Set(reflect.ValueOf(newValue))
}
func main() {
myStruct := MyStruct{123}
SetReflectConcrete(myStruct, "MyField", int32{1234})
}
How can I make a variant of the SetReflect
function that works on any struct? All my attempts so far have failed. The signature would be something like this:
func SetReflectInterface(obj interface{}, fieldName string, newValue interface{})
And is this even possible, when calling it like
SetReflectInterface(myStruct, "MyField", int32{1234})
or would it have to be called like
SetReflectInterface(&myStruct, "MyField", int32{1234})
(After all, interface{}
has a pointer to the struct.)
Aucun commentaire:
Enregistrer un commentaire