为了账号安全,请及时绑定邮箱和手机立即绑定

Go 模板一起迭代多个数组

Go 模板一起迭代多个数组

Go
qq_花开花谢_0 2023-07-31 15:39:59
我在这里阅读了很多与 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 的任意长度,但它们都具有相同的长度。我希望它看起来像这样:
查看完整描述

1 回答

?
达令说

TA贡献1821条经验 获得超6个赞

如果您确定这三个数组具有相同数量的元素,那么您可以这样做:


{{ $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}}

这应该会给你一个想法。


查看完整回答
反对 回复 2023-07-31
  • 1 回答
  • 0 关注
  • 64 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信