orderedmap
A golang data type equivalent to python's collections.OrderedDict
Retains order of keys in maps
Can be JSON serialized / deserialized
Usage
package main
import (
"encoding/json"
"github.com/iancoleman/orderedmap"
)
func main() {
o := orderedmap.New()
o.SetEscapeHTML(false)
o.Set("a", 1)
o.Set("b", "\\.<>[]{}_-")
val, ok := o.Get("a")
keys := o.Keys()
for _, k := range keys {
v, _ := o.Get(k)
}
o.Delete("a")
bytes, err := json.Marshal(o)
prettyBytes, err := json.MarshalIndent(o, "", " ")
s := `{"a": 1}`
err := json.Unmarshal([]byte(s), &o)
o.SortKeys(sort.Strings)
o.Sort(func(a *orderedmap.Pair, b *orderedmap.Pair) bool {
return a.Value().(float64) < b.Value().(float64)
})
}
Caveats
Tests
go test
Alternatives
None of the alternatives offer JSON serialization.