以下是工作代码的片段。我正在使用杜松子酒模板引擎。
c.HTML(200, "index", gin.H{
"title": "Welcome",
"students": map[int]map[string]string{1: {"PID": "1", "Name": "myName"}},})
在索引模板中我有:
<TABLE class= "myTable" >
<tr class="headingTr">
<td>Name</td>
</tr>
{{range $student := .students}}
<td>{{$student.Name}}</td>
{{end}}
</TABLE>
如您所见,我已经硬编码了students标题(地图)上的值。我想从我构建的休息 API 中获取这些数据。我的 rest API 的响应是一个数组:
[
{
"id": 1,
"name": "Mary"
},
{
"id": 2,
"name": "John"
}
]
我可以将此 JSON 响应解组为map[string]string而不是map[int]map[string]string. 如何将这个未编组的主体传递给学生的参数值,然后在这个数组上迭代索引模板?
肥皂起泡泡
相关分类