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 關注
- 150 瀏覽
添加回答
舉報
0/150
提交
取消