如何压缩两个数组

How to zip two arrays

我从结构中的两个数组开始,然后交给模板引擎。 第一个数组是名字列表,第二个数组是姓氏列表。

如何让 Golang 模板打印全名列表?

我有:

package main
import (
    "os"
    "text/template"
)
func main() {
    type Monty struct {
        FirstName [5]string
        LastName  [5]string
    }
    python := Monty{
        FirstName: [5]string{"John", "Eric", "Terry", "Terry", "Michael"},
        LastName:  [5]string{"Cleese", "Idle", "Gilliam", "Jones", "Palin"},
    }
    t := template.Must(template.New("t3").Parse(`
    {{ range .FirstName }}
      {{.}}
    {{ end }}
`))
    t.Execute(os.Stdout, python)
}

预期的结果是

John Cleese
Eric Idle
Terry Gilliam
Terry Jones
Michael Palin

我尝试过使用“索引”、“管道”和“范围”等等。

如果确定两个数组的长度相同,可以这样做:

 {{ range $index, $fname := .FirstName }}
      {{$fname}} {{index $.LastName $index}}
 {{ end }}