Skip to content

Latest commit

 

History

History
39 lines (28 loc) · 970 Bytes

README.md

File metadata and controls

39 lines (28 loc) · 970 Bytes

Go Reference

Golang Ordered Map

Golang Ordered Map is a map data structure that maintains the order of the keys. It also supports JSON and YAML marshalling.

Installation

go get github.com/nicolasparada/go-ordered-map

Usage

package main

import (
	orderedmap "github.com/nicolasparada/go-ordered-map"
)

func main() {
	data := []byte(`{ "name": "John", "age": 30, "active": true }`)

	var unordered map[string]any{}
	if err := json.Unmarshal(data, &unordered); err != nil {
		panic(err)
	}

	var ordered orderedmap.OrderedMap[string, any]
	if err := json.Unmarshal(data, &ordered); err != nil {
		panic(err)
	}

	json.NewEncoder(os.Stdout).Encode(unordered) // will print in undefined order
	json.NewEncoder(os.Stdout).Encode(ordered) // will always print: {"name":"John","age":30,"active":true}
}