To access field values of a struct given by their names, you need help from the reflect
package. It can be done like this:
v := AdFile{ID:1, Title: "T1"} // A struct value
name := "ID" // field name
fieldValue := reflect.ValueOf(v).FieldByName(name).Interface()
Since this is Go code, you can't embed this in templates. But you may register custom functions with the Template.Funcs()
method which may be called from templates.
So let's do this: wrap this functionality into a function, and register it by the name "Field"
so we can call it from our template.
func main() {
t := template.Must(template.New("").Funcs(template.FuncMap{
"Field": func(v interface{}, name string) interface{} {
return reflect.ValueOf(v).FieldByName(name).Interface()
},
}).Parse(templ))
m := map[string]interface{}{
"columns": []string{"ID", "Title"},
"dataList": []AdFile{
{ID: 1, Title: "Title1"},
{ID: 2, Title: "Title2"},
},
}
if err := t.Execute(os.Stdout, m); err != nil {
panic(err)
}
}
const templ = `{{$columns := .columns}}
{{range $dx := .dataList}}
{{range $c := $columns}}
{{- Field $dx $c }}
{{end}}
{{end}}`
Output of the above app (try it on the Go Playground):
1
Title1
2
Title2
Note: error check is omitted in the registered "Field"
function. You may improve it to return nil
if the given field name is invalid, or return an error which is handled by the template engine (in which case template execution would be aborted with the error you return).