我有一個結構: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 關注
- 141 瀏覽
添加回答
舉報
0/150
提交
取消