api 的 json 响应是这样的{ "ResponseCode": "1", "Response": "Data Found", "data": [ { "Season": "KHARIF", "Sector": "AGRICULTURE", "Category": "Cereals", "Crop": "Paddy (Dhan)", "QueryType": "\tField Preparation\t", "QueryText": "top dressing for paddy", "KccAns": "top dressing for paddy : apply urea 25kg+SSP 15kg + neem cake 5kg+MN mixture 5kg mixed with 40kg of sand", "StateName": "PUDUCHERRY", "DistrictName": "KARAIKAL", "BlockName": "KARAIKAL", "CreatedOn": "1/5/2014 6:48:09 PM" }, { "Season": "KHARIF", "Sector": "AGRICULTURE", "Category": "Others", "Crop": "Others", "QueryType": "Weather", "QueryText": "weather forecasting details", "KccAns": "today no rain", "StateName": "PUDUCHERRY", "DistrictName": "KARAIKAL", "BlockName": "KARAIKAL", "CreatedOn": "1/5/2014 9:04:50 PM" }, { "Season": "KHARIF", "Sector": "AGRICULTURE", "Category": "Others", "Crop": "Others", "QueryType": "0", "QueryText": "details about soil testing", "KccAns": "contact to agricultural department", "StateName": "PUDUCHERRY", "DistrictName": "KARAIKAL", "BlockName": "KARAIKAL", "CreatedOn": "1/8/2014 10:21:18 AM" }, { "Season": "KHARIF", "Sector": "AGRICULTURE", "Category": "Cereals", "Crop": "Paddy (Dhan)", "QueryType": "Fertilizer Use and Availability", "QueryText": "paddy top dressing fertilizer", "KccAns": "apply urea 25 kg + potash 15 kg + neem cake 5 kg + microfood 5 kg / ac", "StateName": "PUDUCHERRY", "DistrictName": "KARAIKAL", "BlockName": "KARAIKAL", "CreatedOn": "1/12/2014 8:01:45 AM" } ]}我正在尝试编写一个 golang 模板,它仅返回响应对象数据部分中的前两个数据点。{{range $element := .data}} {{$element}} {{end}}这是我目前正在使用的模板,但这会返回 .data 字段中的所有子数据。我怎样才能进行这个锻炼。
1 回答
RISEBY
TA贡献1856条经验 获得超5个赞
您可以使用slice模板函数获取前两个元素。例子:
{{$dataSliced := slice .data 0 2}}
{{range $element := $dataSliced}}
{{$element}}
{{end}}
或者您也可以为切片创建自定义模板函数。
- 1 回答
- 0 关注
- 99 浏览
添加回答
举报
0/150
提交
取消