我有一个结构:type Records []struct { Example string `json:"example"` Data string `json:"data"`}type Response struct { RecordsCount int `json:"recordsCount"` Records []Records `json:"records"`}但我想在折叠的 html 部分中显示它,如下所示:<div class="collapse"> <input type="checkbox" id="collapse-section1" checked aria-hidden="true"> <label for="collapse-section1" aria-hidden="true">Collapse section 1</label> <div> <p>This is the first section of the collapse</p> </div> <input type="checkbox" id="collapse-section2" aria-hidden="true"> <label for="collapse-section2" aria-hidden="true">Collapse section 2</label> <div> <p>This is the second section of the collapse</p> </div></div>但为了做到这一点,我需要某种 ID 来替换实际id=""的labeland input。但这就是问题所在,我的结构没有 ID;有时数据是重复的(我希望将其包含在内),因此为了正确执行此操作,我必须生成一个 ID,然后将其附加到切片中,然后这样做。我不知道该怎么做。有人可以指出我正确的方向吗?
1 回答
红颜莎娜
TA贡献1842条经验 获得超13个赞
使用 Go 模板执行此操作的一种方法是使用数组索引作为 id 的一部分:
{{range $index,$item := .response}}
<input type="checkbox" id="collapse-section{{$index}}" checked aria-hidden="true">
...
- 1 回答
- 0 关注
- 171 浏览
添加回答
举报
0/150
提交
取消
