में एचटीएमएल टेम्पलेट पर संख्या प्रदर्शित जाओ में html/टेम्पलेट का उपयोग करना आप कर सकते हैं।निम्नलिखित जाओ
उत्तर
की Variables
अनुभाग मेरी html template में:
<table class="table table-striped table-hover" id="todolist">
{{range $index, $results := .}}
<tr>
<td>{{add $index 1}}</td>
<td>{{.Title}}</td>
<td>{{.Description}}</td>
</tr>
{{end}}
</table>
जाने कोड में मैं एक समारोह जो मैं FuncMap के लिए पारित लिखा है:
func add(x, y int) int {
return x + y
}
मेरी हैंडलर में:
type ToDo struct {
Id int
Title string
Description string
}
func IndexHandler(writer http.ResponseWriter, request *http.Request) {
results := []ToDo{ToDo{5323, "foo", "bar"}, ToDo{632, "foo", "bar"}}
funcs := template.FuncMap{"add": add}
temp := template.Must(template.New("index.html").Funcs(funcs).ParseFiles(templateDir + "/index.html"))
temp.Execute(writer, results)
}
मैं HTML/टेम्पलेट का उपयोग नहीं कर रहा हूं पाठ/टेम्पलेट – jwesonga
'html/template' उन्हें 'टेक्स्ट/टेम्पलेट' पर पास करने से पहले मूल्यों से बच निकलता है। यदि आप 'एचटीएमएल/टेम्पलेट' के लिए दस्तावेज़ देखते हैं, तो आप देखेंगे कि वे आपको 'टेक्स्ट/टेम्पलेट' के लिए दस्तावेज़ों में आसानी से संदर्भित करते हैं। – dskinner
'" text/टेम्पलेट "' (इस प्रकार भी '" html/टेम्पलेट "') किसी भी गणना को लागू नहीं करता है। आपको अपने स्वयं के गो फ़ंक्शन को कार्यान्वित करने की आवश्यकता है जिसे आप टेम्पलेट से कॉल करते हैं। ज्यादातर मामलों में 'रेंज' कार्रवाई द्वारा लौटाई गई सूचकांक पर्याप्त है। – snap