Tuesday, July 8, 2025
HomeGolangTemplate : Map with dynamic index - Getting Assist

Template : Map with dynamic index – Getting Assist


Hello

I’ve a map like this

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

information["item_0"] = ...

how do I entry the map with out utilizing the vary in template?
{{index . "item_0"}}

I’ve a variety contained in the template for an additional set of things

{{vary $key, $worth := .customers}} I would like the map worth inside this vary. In line with the important thing index .gadgets "item_$key" {{finish}}

How do I entry the worth of the map right here?

Does this be just right for you?

package deal predominant

import (
	"fmt"
)

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

	fmt.Printf("Worth of the second row: %vn", rows[1]["key"])
}



1 Like

However how will we this in template? .html file
with out utilizing any loop?

{{vary $key, $worth := .consumer}}
{{`item_$key`}}

it prints simply “item_$key” within the template.
item_0
item_1
isn’t printed.

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

Hello

I discovered it.

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



1 Like

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments