猿问

生成唯一 ID 并附加到 Slice

我有一个结构:


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,然后将其附加到切片中,然后这样做。


我不知道该怎么做。有人可以指出我正确的方向吗?


蝴蝶不菲
浏览 172回答 1
1回答

红颜莎娜

使用 Go 模板执行此操作的一种方法是使用数组索引作为 id 的一部分:{{range $index,$item := .response}}&nbsp; <input type="checkbox" id="collapse-section{{$index}}" checked aria-hidden="true">...
随时随地看视频慕课网APP

相关分类

Go
我要回答