Reputation: 19208
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.)
Upvotes: 1
Views: 42
Reputation: 120941
Declare the argument as type interface{}
as you noted. Pass a pointer to the struct as in the last code snippet.
func SetReflectConcrete(obj interface{}, fieldName string, newValue interface{}) {
objElem := reflect.ValueOf(obj).Elem()
field := objElem.FieldByName(fieldName)
field.Set(reflect.ValueOf(newValue))
}
myStruct := MyStruct{123}
SetReflectConcrete(&myStruct, "MyField", int32(1234))
The reflect value must be addressable to set a field. The value will not be addressable if created directly from a struct.
Upvotes: 1