Reputation: 331
I have something map by . (point symbol) and I want to print just every key. I know we can use some:
{{ range $key, $value := . }}{{ $key }}{{end}}
But I cant use var, because I use Docker Compose file which problem withi symbol $.
How I can print all keys without use vars?
Upvotes: 1
Views: 2973
Reputation: 9633
Extract the keys and sort them, then feed them into the view, as iterating a map has indeterminate order anyway (which you don’t want).
import "sort"
var m map[int]string
var keys []int
for k := range m {
keys = append(keys, k)
}
sort.Ints(keys)
Within a range loop, use . to refer to the items being ranged. A new context is created for that loop.
{{ range . }}
{{ . }}
{{ end }}
However I suggest you fix the problem with using the symbol $, you will want to use that. That sounds like the wrong use of Docker Compose files - you could put your golang templates into separate files, or into the go source files themselves if you want a single binary deploy.
Upvotes: 2