I have a map, which I create from a slice of strings. I then want to marshal this into bson format, to insert into mongodb as an index. However, because of how maps are created in Golang, I get a different ordering of the index each time (sometimes its abc, othertimes its bac, cba...).
How can I ensure that the marshalled index created is always in the same order?
fields := ["a", "b", "c"]
compoundIndex := make(map[string]int)
for _, field := range fields {
compoundIndex[field] = 1
}
data, err := bson.Marshal(compoundIndex)
fmt.Println(string(data)) // This output is always in a different order than the desired abc