如何索引切片元素?

Dea*_*ean 44 slice go-templates

我有一个片段: Keys []* datastore.Key

我怎么能在模板文件中索引其中一个?我猜{{.Keys[3] }},但这不起作用,我搜索了很多,但没有任何线索.

任何建议都会受到欢迎,谢谢.

nem*_*emo 71

index像这样使用命令:

{{index .Keys 3}}
Run Code Online (Sandbox Code Playgroud)


dsk*_*ner 33

如html/template包中所述,大多数示例实际上位于text/template pkg docs中.见http://golang.org/pkg/text/template/

来自文档

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.
Run Code Online (Sandbox Code Playgroud)