Go 模板中数组索引的范围

Go 模板中数组索引的范围,go,Go,我知道您可以在范围内使用索引: {{range $i, $e := .First}}$e - {{index $.Second $i}}{{end}} 发件人: 如果索引还包含数组,如何在索引上设置范围 例如 index.html {{range $i, $a := .Title}} {{index $.Article $i}} // Want to range over this. {{end}} 您可以使用嵌套循环,就像编写代码一样 下面是一些代码,演示 不清楚你的意思。“索引”是

我知道您可以在范围内使用索引:

{{range $i, $e := .First}}$e - {{index $.Second $i}}{{end}}
发件人:

如果索引还包含数组,如何在索引上设置范围

例如

index.html

{{range $i, $a := .Title}}
  {{index $.Article $i}}  // Want to range over this.
{{end}}

您可以使用嵌套循环,就像编写代码一样

下面是一些代码,演示


不清楚你的意思。“索引”是一个
int
。你能展示包含你的数据的结构/切片定义的相关部分吗?哇,非常感谢!我尝试了一会儿不同的语法,但错过了添加
$article:=
{{range $i, $a := .Title}}
  {{index $.Article $i}}  // Want to range over this.
{{end}}
package main

import (
    "html/template"
    "os"
)

type a struct {
    Title   []string
    Article [][]string
}

var data = &a{
    Title: []string{"One", "Two", "Three"},
    Article: [][]string{
        []string{"a", "b", "c"},
        []string{"d", "e"},
        []string{"f", "g", "h", "i"}},
}

var tmplSrc = `
{{range $i, $a := .Title}}
  Title: {{$a}}
  {{range $article := index $.Article $i}}
    Article: {{$article}}.
  {{end}}
{{end}}`

func main() {
    tmpl := template.Must(template.New("test").Parse(tmplSrc))
    tmpl.Execute(os.Stdout, data)
}