I have a struct like this:
type User struct {
Name string
UID int
Bio string
}
I have a given instantiated struct, and I want to loop through the fields in that object and modify them one by one.
This is what I have so far
user := User{
Name: "Test",
UID: 1,
Bio: "Test bio",
}
reflectVal := reflect.ValueOf(user)
numFields := reflectVal.NumField()
for i := 0; i < numFields; i++ {
fieldType := reflect.TypeOf(reflectVal.Field(i))
reflectVal.Field(i).Set(reflect.Zero(fieldType))
...
}
But I'm getting this error:
panic: reflect: reflect.Value.SetString using unaddressable value
Is there a way to do this?
Aucun commentaire:
Enregistrer un commentaire