My problem: I have a map[string]Type which I want to iterate over in a template maintaining the random ordering behaviour of the wider language.
The Go template library states here https://golang.org/pkg/text/template/#hdr-Actions that :
If the value is a map and the keys are of basic type with a defined order ("comparable"), the elements will be visited in sorted key order.
I know I can work around this by declaring a separate []string of the keys in the initial map, then iterate over this, i.e.:
data := map[string]DummyStruct{}
data["Windward"] = DummyStruct{"Windward", 15}
data["Phlebas"] = DummyStruct{"Phlebas", 3}
data["Art"] = DummyStruct{"Art", 3}
i := 0
indices := make([]string, len(data))
for name, value := range data {
fmt.Printf("%v, %v\n", name, value)
indices[i] = name
i ++
}
however I was hoping for this being a fully native, supported feature of the templating library to match the behaviour across the wider language, however it doesn't seem to be supported at all.
See the Playground here for a full example: https://play.golang.org/p/1oTI56G5pr9