Go 模板一起迭代多个数组

我在这里阅读了很多与 Go 模板相关的帖子,但找不到我要找的内容。


我有这两个结构,我无法更改它们,因为它们在我正在帮助的项目的其他地方使用:


type Schedule struct {

    Description   string

    ControlNights int

    PlayNights    int

    StartDay      int

    Combos        []Combo

}


type Combo struct {

    From    time.Time

    Every   int

    Until   time.Time

    Sounds  []string

    Volumes []int

    Waits   []int

}

我需要在 html 页面上显示这些数据。所以我需要迭代每个 Combo。我可以做到这一点,但我需要将声音、音量和等待放在一起,即声音 x 音量 y 等待 z。其中 x、y 和 z 是 Sounds、Volumes 和 Waits 数组中的值。这些数组可以是 >= 1 的任意长度,但它们都具有相同的长度。


我希望它看起来像这样:

https://img3.mukewang.com/64c7656a0001cb3a04450223.jpg

qq_花开花谢_0
浏览 67回答 1
1回答

达令说

如果您确定这三个数组具有相同数量的元素,那么您可以这样做:{{ $volumes := .Volumes }}{{ $waits := .Waits }}{{range $index,$sound := .Sounds }}Print Sounds[i] like this: {{$sound}}Print volumes[i] like this: {{index $volumes $index}}Print waits[i] like this: {{index $waits $index}}{{end}}这应该会给你一个想法。
打开App,查看更多内容
随时随地看视频慕课网APP