go - Cast interface{} to type in template -


  templates.executetemplate(w, "index.html", map[string]interface{} {         "games": games}) } 

where games []map[string]interface{} (mapped result of sql query)

in template:

{{ range $gval := .games }}      {{ how make like: $gval.name.(string) }}  {{end}} 

how cast interface{} value of map string(or int) in template? in 'go' can games[0]["name"].(string)

when $gval.name writes hex string

i don't think it's possible type assertions template. you'll have write own function , call template. example:

func tostring(value interface{}) string {     switch v := value.(type) {     case string:         return v     case int:         return strconv.itoa(v)     // add whatever other types need     default:         return "" } 

to able call function template have call funcs() method on template:

tpl.funcs(template.funcmap{"tostring": tostring}) 

now can {{$gval.name | tostring}} inside template


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -