This is my struct:
type TableFields struct {
Name string
Family string
Age int
}
sample := TableFields{
Name: "bill",
Family: "yami",
Age: 25,
}
This is a very simple sample that I am using to describe my problem.
I want to change values in the sample
struct using keys and values in a map
that I receive. Each time I receive the map
the keys and values will be different. How can I use the map
to edit the sample
struct?
For example:
updateTheseFieldsWithTheseVals := make(map[string]string)
updateTheseFieldsWithTheseVals["family"] = "yamie"
// this is my way
for key,val := range updateTheseFieldsWithTheseVals {
// sample.Family=yamie works, but is not the answer I am looking for
// sample.key = val *This solution is not possible*
oldValue := reflect.Indirect(reflect.ValueOf(get)).FieldByName(key).String()
fmt.Println(oldValue) // result is yami
oldValue = val
fmt.Println(oldValue) //result is yamie
}
fmt.Println(updateTheseFieldsWithTheseVals)
// result :
// {bill yami 25}
This runs but does not change the values in sample
.