Template : Map with dynamic index


I have a map like this

data := map[string]interface{}{}

data["item_0"] = ...

how do I access the map without using the range in template?
{{index . "item_0"}}

I have a range inside the template for another set of items

{{range $key, $value := .users}} I need the map value inside this range. According to the key index .items "item_$key" {{end}}

How do I access the value of the map here?

Does this work for you?

package main

import (

func main() {
	rows := []map[string]interface{}{
		{"key": "value1"},
		{"key": "value2"},
		{"key": "value3"},

	fmt.Printf("Value of the second row: %v\n", rows[1]["key"])
1 Like

But how do we this in template? .html file
without using any loop?

{{range $key, $value := .user}}

it prints just “item_$key” in the template.
is not printed.

How do I convert that $key to 0, 1, 2 … and add it with string value in template?


I found it.

(printf "%v%v" "item_" $key)

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.