Skip to content
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.
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE First commit Mar 1, 2017
orderedmap_test.go Add test for marshalling value as an empty map Mar 18, 2019 Syntax highlight Go code in readme Dec 26, 2018


A golang data type equivalent to python's collections.OrderedDict

Retains order of keys in maps

Can be JSON serialized / deserialized


package main

import (

func main() {

    // use New() instead of o := map[string]interface{}{}
    o := orderedmap.New()

    // use Set instead of o["a"] = 1
    o.Set("a", 1)

    // use Get instead of i, ok := o["a"]
    val, ok := o.Get("a")

    // use Keys instead of for k, v := range o
    key := o.Keys()
    for _, k := range keys {
        v, _ := o.Get(k)

    // use o.Delete instead of delete(o, key)
    err := o.Delete("a")

    // serialize to a json string using encoding/json
    bytes, 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 orderedmaps
    s := `{"a": 1}`
    err := json.Unmarshal([]byte(s), &o)
    // sort the keys
    // sort by Pair
    o.Sort(func(a *Pair, b *Pair) bool {
        return a.Value().(float64) < b.Value().(float64)


  • OrderedMap only takes strings for the key, as per the JSON spec.


go test


None of the alternatives offer JSON serialization.

You can’t perform that action at this time.