gpt4 book ai didi

go - 生成唯一ID并附加到Slice

转载 作者:行者123 更新时间:2023-12-01 21:09:34 25 4
gpt4 key购买 nike

我有一个结构:

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=""label的实际 input
但这就是问题,我的结构没有ID。有时数据是重复的(我希望将其包括在内),因此为了正确执行此操作,我必须生成一个ID,然后将其附加到 slice 上,并以此方式进行操作。
我不知道该怎么做。有人可以指出我正确的方向吗?

最佳答案

使用Go模板执行此操作的一种方法是将数组索引用作id的一部分:

{{range $index,$item := .response}}
<input type="checkbox" id="collapse-section{{$index}}" checked aria-hidden="true">
...

关于go - 生成唯一ID并附加到Slice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63201675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com