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

生成唯一 ID 并附加到 Slice

生成唯一 ID 并附加到 Slice

Go
蝴蝶不菲 2022-06-21 15:50:52
我有一个结构: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">

...


查看完整回答
反对 回复 2022-06-21
  • 1 回答
  • 0 关注
  • 171 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号