Show your code. I don't understand what you're saying. I suspect the root problem has nothing to do with templates.
-rob
func deviceSelected(device string, devices interface{}) bool {
if device == "" {
panic("Need device")
}
if devices == nil {
panic("Need devices")
}
vices, err := devices.([]interface{})
if err {
panic("type conversion failed")
}
log.Print(vices)
for _, d := range vices {
y, err := d.(Device)
if err {
panic("Type conversion failed.")
}
log.Printf("%s == %s", device, y.Address)
if device == y.Address {
return true
}
}
return false
}
{{ range $.User.Devices }}<li data-address="{{ .Address }}"{{ if deviceSelected .Address $.Links.Devices }} class="primary"{{ end }}>{{ .Name }}</li>{{ end }}
[] <---- empty slice from log.Print(vices)
I added comments to one fragment of code to explain what I think is going on:vices, err := devices.([]interface{}) // evaluates to nil, false because devices is type []*Device
if err {// never get here because err is false
panic("type conversion failed")
}
log.Print(vices) // nil slices print as "[]"
One thing that might be causing confusion is that the second result of a type assertion is a boolean, not an error. The boolean is true if the assertion holds. This fragment of code panics when devices is of type []interface.
vices, err := devices.([]interface{})
if err {
panic("type conversion failed")
}
The code will be clearer if you s/err/ok/,