I am outputting a dropdown list using a html template, as follows:
{{range .Organisations}}
<option value="{{.Uuid}}">{{.Name}}</option>
{{end}}
I want to indicate that one of the items should be selected by doing an eq
comparator. The only potential complication I am aware of is that the .Uuid
is a *gocql.UUID
, I am trying to do this:
{{range .Organisations}}
<option value="{{.Uuid}}"{{if eq $.Contact.Organisation .Uuid}} selected="selected"{{end}}>{{.Name}}</option>
{{end}}
But it results in the following error message:
page:32:36: executing "submit_scholarship" at : error calling eq: invalid type for comparison
Any pointers in the right direction would be much appreciated.
eq only works on basic types. You can add a custom function to do this. http://play.golang.org/p/rkYnlqmeLA
package main
import (
"html/template"
"os"
)
type s struct {
Name []byte
Uuid []byte
}
func main() {
data := s{[]byte("aa"), []byte("aa")}
funcMap := template.FuncMap{
"equals": func(a []byte, b []byte) bool {
return string(a) == string(b)
},
}
var html = `{{if equals .Name .Uuid }}hi{{end}}`
tmpl, _ := template.New("test").Funcs(funcMap).Parse(html)
err := tmpl.Execute(os.Stdout, data)
if err != nil {
println(err.Error())
}
}