有没有办法将一个变量(字符串,int,bool)传递给模板。例如(类似的东西):
import (
"html/template"
)
func main() {
....
tmpl := template.Must(template.ParseFiles("templates/index.html"))
mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
varmap := map[string]interface{}{
"var1": "value",
"var2": 100,
}
tmpl.ExecuteTemplate(rw, "index", varmap)
})
// content of index.html
{{define "index"}}
{{var1}} is equal to {{var2}}
{{end}}
}