Skip to content

Latest commit

History

39 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

orderedmap

Build Status

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"
)
funcmain() {
// use New() instead of o := map[string]interface{}{}o:=orderedmap.New()
// use SetEscapeHTML() to whether escape problematic HTML characters or not, defaults is trueo.SetEscapeHTML(false)
// use Set instead of o["a"] = 1o.Set("a", 1)
// add some value with special characterso.Set("b", "\\.<>[]{}_-")
// use Get instead of i, ok := o["a"]val, ok:=o.Get("a")
// use Keys instead of for k, v := range okeys:=o.Keys()
for_, k:=rangekeys {
v, _:=o.Get(k)
}
// use o.Delete instead of delete(o, key)o.Delete("a")
// serialize to a json string using encoding/jsonbytes, err:=json.Marshal(o)
prettyBytes, err:=json.MarshalIndent(o, "", " ")
// deserialize a json string using encoding/json// all maps (including nested maps) will be parsed as orderedmapss:=`{"a": 1}`err:=json.Unmarshal([]byte(s), &o)
// sort the keyso.SortKeys(sort.Strings)
// sort by Pairo.Sort(func(a*orderedmap.Pair, b*orderedmap.Pair) bool {
returna.Value().(float64) <b.Value().(float64)
})
}

Caveats

Tests

go test

Alternatives

None of the alternatives offer JSON serialization.

About

orderedmap is a golang map where the keys keep the order that they're added. It can be de/serialized from/to JSON. It's based closely on the python collections.OrderedDict.

Resources

Stars

406 stars

Watchers

6 watching

Forks

Releases

Packages

Used by

Contributors

Languages