注意:您可以在 Go Playground中
尝试/签出您工作的修改后的代码。
如果希望
template软件包能够访问字段,则必须导出字段。您可以通过以大写字母开头的字段来导出该字段:
type Task struct { cmd string args []string Desc string}请注意,我仅
Desc在此处进行了更改,您必须将要在模板中引用的所有其他字段都大写。
导出之后
Desc,请当然将所有引用更改为大写:
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", },}并且在模板中:
<html>{{range $key, $value := .}} <li>Task Name: {{$key}}</li> <li>Task Value: {{$value}}</li> <li>Task description: {{$value.Desc}}</li>{{end}}</html>输出:
<html><li>Task Name: find</li><li>Task Value: {find [/tmp/] find files in /tmp dir}</li><li>Task description: find files in /tmp dir</li><li>Task Name: grep</li><li>Task Value: {grep [foo /tmp/* -R] grep files match having foo}</li><li>Task description: grep files match having foo</li></html>


