Does anybody can help me with it: I need to set up field values for different types of structures. I have a map with data, extracted from the database. In this particular function, I want to create an object of any structure, fields of which are matched with the map
type Member struct {
firstName string `xml: "FIRST_NAME"`
lastName string `xml: "LAST_NAME"`
}
type CardData struct {
cardType string `xml: "CARD_TYPE"`
cardNumber string `xml: "CARD_NUMBER"`
}
func main() {
fields := make(map[string]string)
fields['CARD_TYPE'] = "VISA"
fields['FIRS_NAME'] = "Aria Stark"
member := Combiner(fields, Member{})
card := Combiner(fields, CardData{})
}
func Combiner(m map[string]string, obj interface{}) interface{} {
ff := reflect.ValueOf(obj)
typeOfS := ff.Type()
for i := 0; i< ff.NumField(); i++ {
tag := typeOfS.Field(i).Tag.Get("xml")
if _, ok := m[tag]; ok {
n := typeOfS.Field(i).Name
reflections.SetField(&obj, n, m[tag])
} else {
fmt.Printf("The field %s is not found \n", tag)
}
}
return obj
}
but I get an error in this string "reflections.SetField(&obj, n, m[tag])" It doesn't work because "obj" is not a struct
Many thanks for all yours answers!