亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

生成唯一 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 關注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號