Evgeniy Berezovsky
Evgeniy Berezovsky

Reputation: 19208

Reflectively set field of struct that was passed in as interface{}

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

Answers (1)

Thundercat
Thundercat

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))

Run it on the playground.

The reflect value must be addressable to set a field. The value will not be addressable if created directly from a struct.

Upvotes: 1

Related Questions