I am trying to process checkboxes in go template forms…
What is the efficient way to check if a checkbox is checked from an array of options?
I have:
data := make(map[string]interface{})
data["User"] = user
data["Roles"] = []string{"User", "Admin"}
tmpl := template.Must(template.ParseFiles("templates/admin/user.gohtml"))
tmpl.Execute(w, data)
And want to parse it like this:
{{range .Roles}}
<input name="roles" type="checkbox" id="roles{{.}}" value="{{.}}"
{{if eq . .User.Roles}}checked{{end}}/>
<label for="roles{{.}}">{{.}}</label>
{{end}}
So… To check if it is checked efficiently, I need an “in” operator. i.e. {{if in . .User.Roles}}
. I’m sure I can solve it the hard way. But what is the correct way? They must have thought of this…
Thanks