I have a Struct Task
:
type Task struct {
cmd string
args []string
desc string
}
And I init a map which take the above Task
struct as a value and a string
as a key(the task name)
var taskMap = map[string]Task{
"find": Task{
cmd: "find",
args: []string{"/tmp/"},
desc: "find files in /tmp dir",
},
"grep": Task{
cmd: "grep",
args:[]string{"foo","/tmp/*", "-R"},
desc: "grep files match having foo",
},
}
and I want to parse a html page using html/template
just using the above taskMap
.
func listHandle(w http.ResponseWriter, r *http.Request){
t, _ := template.ParseFiles("index.tmpl")
t.Execute(w, taskMap)
}
Here is the index.tmpl
:
<html>
{{range $key, $value := .}}
<li>Task Name: {{$key}}</li>
<li>Task Value: {{$value}}</li>
<li>Task description: {{$value.desc}}</li>
{{end}}
</html>
I can get the $key
and value
printed successfully, but When It comes to the field of Task
using {{$value.desc}}
it wont work.
How can I get the desc
of each task
in this case?