html - Go模板一起遍历多个数组

标签 html templates 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页面上显示此数据。所以我需要遍历每个组合。我可以这样做,但是我需要将“声音”,“音量”和“等待”放在一起,即声音x音量y等待z。其中x,y和z是Sounds,Volumes和Waits数组中的值。这些数组的长度可以大于等于1,但是它们的长度都相同。

我希望它看起来像这样:

enter image description here

我已经试过了:
            {{range .Schedule.Combos}}
            <div class="container">
                <div class="row">
                    <div class="col">
                        <div class="card mt-2 ml-2">
                            <div class="card-body">
                                <h5 class="card-title">
                                    <strong>Timing</strong>
                                </h5>
                                <h5 class="card-text">
                                    Play every {{.Every}} mins
                                </h5>
                                <h5 class="card-text">
                                    From time {{TimeOfDayToString .From}}
                                </h5>
                                <h5 class="card-text">
                                    Until {{TimeOfDayToString .Until}}
                                </h5>
                            </div>
                        </div>
                    </div>
                    <div class="col">
                        <div class="card mt-2">
                            <div class="card-body">
                                <h5 class="card-title">
                                    <strong>Sounds</strong>
                                </h5>
                                {{range .Sounds}}
                                    <h5 class="card-text">
                                        Sound {{.}}
                                    </h5>
                                {{end}}
                            </div>
                        </div>
                    </div>

这可以让我对声音进行范围调整,但同时我也需要对音量进行调整并同时等待(等待播放下一个声音的时间),同时以某种方式...

我想我可以做的是制作一个不同的Combo结构并将其传递给我的html模板,但是我希望避免这种情况,只要有一种方法就可以使用。

最佳答案

如果确定这三个数组具有相同数量的元素,则可以执行以下操作:

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

这应该给你的想法。

关于html - Go模板一起遍历多个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58496024/

相关文章:

html - 为什么我的 3d 转换的 div 被剪裁,即使父元素设置为溢出 :visible?

angular - 如何创建带有图标的可重用按钮组件

c++ - 参数包扩展遇到问题

javascript - 从 React Native 调用二进制文件?

go - 如何构建复杂的go数据结构

HTML部分背景视频

html - 媒体查询可以加载外部 html 页面,然后从中加载 css 链接吗?

javascript - HTML 验证器返回 "document type does not allow element "脚本“此处”

c++ - 如何为输入和输出参数使用单独的参数包?

go - 如何在struct Beego中设置unique