text/template: space 在地图的键中
text/template: space in map's key
我有以下代码(使用 text/template
):
inventory := map[string]string{"name of the movie": "hello"}
tmpl, err := template.New("test").Parse("Movie name ") // I want to display "hello" there
if err != nil { panic(err) }
err = tmpl.Execute(os.Stdout, inventory)
if err != nil { panic(err) }
如您所见,我的地图键 name of the movie
中有空格。如何在 parse 参数中显示 hello
(即 name of the movie
的值)?
使用索引函数:Movie name: {{index . "name of the movie"}}
来自the docs:
index
Returns the result of indexing its first argument by the
following arguments. Thus "index x 1 2 3" is, in Go syntax,
x[1][2][3]. Each indexed item must be a map, slice, or array.
我有以下代码(使用 text/template
):
inventory := map[string]string{"name of the movie": "hello"}
tmpl, err := template.New("test").Parse("Movie name ") // I want to display "hello" there
if err != nil { panic(err) }
err = tmpl.Execute(os.Stdout, inventory)
if err != nil { panic(err) }
如您所见,我的地图键 name of the movie
中有空格。如何在 parse 参数中显示 hello
(即 name of the movie
的值)?
使用索引函数:Movie name: {{index . "name of the movie"}}
来自the docs:
index
Returns the result of indexing its first argument by the
following arguments. Thus "index x 1 2 3" is, in Go syntax,
x[1][2][3]. Each indexed item must be a map, slice, or array.