I am new for golang. I met a problem when I use multiplication in html/template. Some code like below.
template code:
{{range $i,$e:=.Items}}
<tr>
<td>{{add $i (mul .ID .Number)}}</td>
<td>{{.Name}}</td>
</tr>
{{end}}
.go code
type Item struct{
ID int
Name string
}
func init() {
itemtpl,_:=template.New("item.gtpl").
Funcs(template.FuncMap{"mul": Mul, "add": Add}).
ParseFiles("./templates/item.gtpl")
}
func itemHandle(w http.ResponseWriter, req *http.Request) {
items:=[]Item{Item{1,"name1"},Item{2,"name2"}}
data := struct {
Items []Item
Number int
Number2 int
}{
Items: items,
Number: 5,
Number2: 2,
}
itemtpl.Execute(w, data)
}
func Mul(param1 int, param2 int) int {
return param1 * param2
}
func Add(param1 int, param2 int) int {
return param1 + param2
}
It will output nothing when I use the code above. But It will output 10 when I use the code outside of array below.
<html>
<body>
{{mul .Number .Number2}}
</html>
</body>
I google a lot. I cannot find the usable like mine. I want to use multiplication in array inside of html/template. Can someone tell me what is wrong with my code?