orderedmap

package module
v2.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 29, 2026 License: MIT Imports: 10 Imported by: 0

README

orderedmap

GitHub Tag Go Reference CI codecov

A generic, thread-safe ordered map for Go that preserves insertion order using linkedlist.

Installation

go get github.com/winebarrel/orderedmap/v2

Requires Go 1.27+.

Usage

package main

import (
	"fmt"
	"slices"

	"github.com/winebarrel/orderedmap/v2"
)

func main() {
	om := orderedmap.New[string, any]()

	om.Set("foo", "bar")
	om.Set("zoo", 100)
	om.Set("baz", true)

	fmt.Println(om)
	//=> *orderedmap.Map[string,interface {}][foo:bar zoo:100 baz:true]

	// Get a value
	fmt.Println(om.Get("foo"))
	//=> bar

	// Get a value with existence check
	v, ok := om.GetOk("foo")
	fmt.Println(v, ok)
	//=> bar true

	// Iterate over key-value pairs in insertion order
	for k, v := range om.All() {
		fmt.Println(k, v)
	}
	//=> foo bar
	//   zoo 100
	//   baz true

	// Iterate over keys
	for k := range om.Keys() {
		fmt.Println(k)
	}

	// Iterate over values
	for v := range om.Values() {
		fmt.Println(v)
	}

	// Push moves an existing key to the back
	om.Push("foo", "new_bar")
	for k, v := range om.All() {
		fmt.Println(k, v)
	}
	//=> zoo 100
	//   baz true
	//   foo new_bar

	// Get all key-value pairs as a slice
	pairs := om.Entries()
	fmt.Println(pairs)
	//=> [{zoo 100} {baz true} {foo new_bar}]

	// Delete a key
	om.Delete("zoo")

	// Get the number of entries
	fmt.Println(om.Len())
	//=> 2

	// Clear all entries
	om.Clear()

	// Create from an iterator
	om2 := orderedmap.From(slices.All([]string{"foo", "bar", "baz"}))
	fmt.Println(om2)
	//=> *orderedmap.Map[int,string][0:foo 1:bar 2:baz]
}

Filter

package main

import (
	"fmt"

	"github.com/winebarrel/orderedmap/v2"
)

func main() {
	om := orderedmap.New[string, int]()
	om.Set("foo", 1)
	om.Set("bar", 2)
	om.Set("baz", 3)

	// Filter returns a new map containing only the entries that satisfy the predicate
	om2 := om.Filter(func(k string, v int) bool {
		return v != 2
	})
	fmt.Println(om2)
	//=> *orderedmap.Map[string,int][foo:1 baz:3]
}

Transform

package main

import (
	"fmt"

	"github.com/winebarrel/orderedmap/v2"
)

func main() {
	om := orderedmap.New[string, int]()
	om.Set("foo", 1)
	om.Set("bar", 2)
	om.Set("baz", 3)

	// Transform returns an iterator of transformed values
	for s := range om.Transform(func(k string, v int) string {
		return fmt.Sprintf("%s=%d", k, v)
	}) {
		fmt.Println(s)
	}
	//=> foo=1
	//   bar=2
	//   baz=3

	// TransformSlice returns a slice of transformed values
	ss := om.TransformSlice(func(k string, v int) string {
		return fmt.Sprintf("%s=%d", k, v)
	})
	fmt.Println(ss)
	//=> [foo=1 bar=2 baz=3]
}

JSON Marshal / Unmarshal

package main

import (
	"encoding/json"
	"fmt"

	"github.com/winebarrel/orderedmap/v2"
)

func main() {
	// Marshal: preserves insertion order
	om := orderedmap.New[string, any]()
	om.Set("z", 3)
	om.Set("a", 1)
	om.Set("m", 2)

	b, _ := json.Marshal(om)
	fmt.Println(string(b))
	//=> {"z":3,"a":1,"m":2}

	// Unmarshal: preserves key order from JSON
	om2 := orderedmap.New[string, any]()
	json.Unmarshal([]byte(`{"z":3,"a":1,"m":2}`), om2)

	for k, v := range om2.All() {
		fmt.Println(k, v)
	}
	//=> z 3
	//   a 1
	//   m 2
}

encoding/json/v2

Map implements MarshalerTo and UnmarshalerFrom, so encoding/json/v2 options (indentation, HTML escaping, ...) are honored.

package main

import (
	"encoding/json/jsontext"
	json "encoding/json/v2"
	"fmt"

	"github.com/winebarrel/orderedmap/v2"
)

func main() {
	// Marshal: preserves insertion order
	om := orderedmap.New[string, any]()
	om.Set("z", 3)
	om.Set("a", 1)
	om.Set("m", 2)

	b, _ := json.Marshal(om, jsontext.WithIndent("  "))
	fmt.Println(string(b))
	//=> {
	//     "z": 3,
	//     "a": 1,
	//     "m": 2
	//   }

	// Unmarshal: preserves key order from JSON
	om2 := orderedmap.New[string, any]()
	json.Unmarshal([]byte(`{"z":3,"a":1,"m":2}`), om2)

	for k, v := range om2.All() {
		fmt.Println(k, v)
	}
	//=> z 3
	//   a 1
	//   m 2
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Transform

func Transform[K comparable, V any, R any](om *Map[K, V], f func(k K, v V) R) iter.Seq[R]

func TransformSlice

func TransformSlice[K comparable, V any, R any](om *Map[K, V], f func(k K, v V) R) []R

Types

type Map

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func From

func From[K comparable, V any](seq iter.Seq2[K, V]) *Map[K, V]

func New

func New[K comparable, V any]() *Map[K, V]

func (*Map[K, V]) All

func (om *Map[K, V]) All() iter.Seq2[K, V]

func (*Map[K, V]) Clear

func (om *Map[K, V]) Clear()

func (*Map[K, V]) Clone

func (om *Map[K, V]) Clone() *Map[K, V]

func (*Map[K, V]) CollectKeys

func (om *Map[K, V]) CollectKeys() []K

func (*Map[K, V]) CollectValues

func (om *Map[K, V]) CollectValues() []V

func (*Map[K, V]) Delete

func (om *Map[K, V]) Delete(k K) V

func (*Map[K, V]) DeleteOk

func (om *Map[K, V]) DeleteOk(k K) (V, bool)

func (*Map[K, V]) Entries

func (om *Map[K, V]) Entries() []Pair[K, V]

func (*Map[K, V]) Filter added in v2.3.0

func (om *Map[K, V]) Filter(f func(k K, v V) bool) *Map[K, V]

func (*Map[K, V]) Get

func (om *Map[K, V]) Get(k K) V

func (*Map[K, V]) GetOk

func (om *Map[K, V]) GetOk(k K) (V, bool)

func (*Map[K, V]) Keys

func (om *Map[K, V]) Keys() iter.Seq[K]

func (*Map[K, V]) Len

func (om *Map[K, V]) Len() int

func (*Map[K, V]) MarshalJSON

func (om *Map[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON implements jsonv1.Marshaler with encoding/json v1 semantics. Encoders aware of jsonv2.MarshalerTo call Map.MarshalJSONTo instead.

func (*Map[K, V]) MarshalJSONTo added in v2.2.0

func (om *Map[K, V]) MarshalJSONTo(enc *jsontext.Encoder) error

MarshalJSONTo implements jsonv2.MarshalerTo. It encodes the map as a JSON object, preserving insertion order.

func (*Map[K, V]) Push

func (om *Map[K, V]) Push(k K, v V)

func (*Map[K, V]) Set

func (om *Map[K, V]) Set(k K, v V)

func (*Map[K, V]) String

func (om *Map[K, V]) String() string

func (*Map[K, V]) Transform added in v2.1.0

func (om *Map[K, V]) Transform[R any](f func(k K, v V) R) iter.Seq[R]

func (*Map[K, V]) TransformSlice added in v2.1.0

func (om *Map[K, V]) TransformSlice[R any](f func(k K, v V) R) []R

func (*Map[K, V]) UnmarshalJSON

func (om *Map[K, V]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements jsonv1.Unmarshaler with encoding/json v1 semantics. Decoders aware of jsonv2.UnmarshalerFrom call Map.UnmarshalJSONFrom instead.

func (*Map[K, V]) UnmarshalJSONFrom added in v2.2.0

func (om *Map[K, V]) UnmarshalJSONFrom(dec *jsontext.Decoder) error

UnmarshalJSONFrom implements jsonv2.UnmarshalerFrom. It decodes a JSON object, preserving the order of its members. Entries already held by the map are discarded.

func (*Map[K, V]) Values

func (om *Map[K, V]) Values() iter.Seq[V]

type Pair

type Pair[K comparable, V any] struct {
	Key   K
	Value V
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL