Go template - syntax for range

In Go template, I have a map setup like this:

{{$key}}map := make(map[string]interface{})

And I want to iterate through the map using this:

{{ range $mapKey, $mapValue := {{$key}}map}}

And I am getting this error:

unexpected "{" in range

Looks like it does not allow nested {{}} inside another {{}}. Is there anyway I can solve this issue ???

Upvotes: 0

Views: 1823

Answers (1)

Burak Serdar
Burak Serdar

Reputation: 51577

You cannot generate variable names to be used in templates using the template engine itself. You seem to be in need of having multiple maps, one for each $key. So, use a map of maps:

m := make(map[string]map[string]interface{})

where m[key] gives the map for the key.

Then you can do:

{{ range $mapKey, $mapValue := (index $.m $.key)}}
...
{{end}}

Upvotes: 3

Related Questions