orderedmap

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: MIT Imports: 2 Imported by: 0

Image README

orderedmap 🐜

Go Reference

Optimal, constant time implementation of ordered maps for Go with a simple API.

Supports modern range-over-function iteration via the standard library iter package.

Usage

Basic operations

Creating an ordered map uses any generic type K comparable, V any:

// equivalent of: make(map[string]int)
m := orderedmap.New[string, int]()

You can specify an initial capacity hint:

// equivalent of: make(map[string]int, 1000)
m := orderedmap.WithCapacity[string, int](1000)

Setting a value:

// equivalent of m["foo"] = 1
m.Set("foo", 1)

Retrieving a value is equally simple, and uses the same bool ok secondary return pattern to indicate whether a value was found in the map:

// equivalent of val, ok := m["foo"]
val, ok := m.Get("foo")

The remaining operations mirror the standard library map:

m.Delete("foo") // equivalent of delete(m, "foo")
m.Clear()       // equivalent of clear(m)
n := m.Len()    // equivalent of len(m)

Iteration ✨

You can simply range across the All() function, which will yield key value pairs based on their insertion order:

for k, v := range m.All() {
    fmt.Printf("k = %v, v = %v\n", k, v)
}

See also Backward() to iterate from newest to oldest instead, as well as the included Keys() and Values() iterators.

Support

This module requires go1.24 or greater.

Comparison with other Go modules

Upon my review, wk8/go-ordered-map appeared to be the best existing library, offering constant time operations and reasonable memory footprint. This module took some design cues from it. That said, there are some intentional design differences -- comparing this module with it, we optimize for:

  • 🐛 Simpler API (less exposed surface area, similar to standard library maps).
  • 🌱 Reduced feature set (no built-in YAML serialization, for example).
  • ✨ Use modern range-over-function expressions for easy iteration.
  • ⚡ Equally performant.
  • 0⃣ Zero dependencies.
Other alternatives

As per other options, the README from wk8/go-ordered-map offers a summary:

  • iancoleman/orderedmap only accepts string keys, its Delete operations are linear.
  • cevaris/ordered_map uses a channel for iterations, and leaks goroutines if the iteration is interrupted before fully traversing the map.
  • mantyr/iterator also uses a channel for iterations, and its Delete operations are linear.
  • samdolan/go-ordered-map adds unnecessary locking (users should add their own locking instead if they need it), its Delete and Get operations are linear, iterations trigger a linear memory allocation.

Image Documentation

Overview

Package orderedmap implements an ordered map, i.e. a map that also keeps track of the order in which keys were inserted.

All operations are constant-time.

Example
package main

import (
	"fmt"

	"github.com/mroth/orderedmap"
)

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

	value, ok := m.Get("foo")
	if ok {
		fmt.Println(value)
	}
}
Output:
1

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OrderedMap

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

OrderedMap is an ordered map that holds key value pairs and is able to iterate over values based on insertion order.

An OrderedMap is not safe for concurrent use by multiple goroutines.

func Collect added in v0.3.0

func Collect[K comparable, V any](seq iter.Seq2[K, V]) *OrderedMap[K, V]

Collect builds a new OrderedMap from the key-value pairs in seq, preserving the order in which seq yields them. If seq yields a key more than once, the last value wins and the key keeps its first-seen position, matching Set.

func New

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

New creates a new ordered map.

func WithCapacity

func WithCapacity[K comparable, V any](n int) *OrderedMap[K, V]

WithCapacity creates a new ordered map with a capacity hint of n, similar to make(map[K]V, n).

func (*OrderedMap[K, V]) All added in v0.2.0

func (m *OrderedMap[K, V]) All() iter.Seq2[K, V]

All returns an iterator over key-value pairs from m. The ordering will be oldest to newest, based on when a key was first set.

Example
package main

import (
	"fmt"

	"github.com/mroth/orderedmap"
)

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

	for k, v := range m.All() {
		fmt.Printf("k = %v, v = %v\n", k, v)
	}
}
Output:
k = foo, v = 1
k = bar, v = 2
k = baz, v = 3

func (*OrderedMap[K, V]) Backward added in v0.2.0

func (m *OrderedMap[K, V]) Backward() iter.Seq2[K, V]

Backward returns an iterator over key-value pairs from m in reverse. The ordering will be newest to oldest, based on when a key was first set.

Example
package main

import (
	"fmt"

	"github.com/mroth/orderedmap"
)

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

	for k, v := range m.Backward() {
		fmt.Printf("k = %v, v = %v\n", k, v)
	}
}
Output:
k = baz, v = 3
k = bar, v = 2
k = foo, v = 1

func (*OrderedMap[K, V]) Clear added in v0.2.1

func (m *OrderedMap[K, V]) Clear()

Clear removes all entries from the map, leaving it empty.

func (*OrderedMap[K, V]) Delete

func (m *OrderedMap[K, V]) Delete(key K)

Delete deletes the value for a key.

func (*OrderedMap[K, V]) Get

func (m *OrderedMap[K, V]) Get(key K) (value V, ok bool)

Get returns the value stored in the map for a key, or the zero value of V if no value is present. The ok result indicates whether value was found in the map.

func (*OrderedMap[K, V]) Keys added in v0.2.0

func (m *OrderedMap[K, V]) Keys() iter.Seq[K]

Keys returns an iterator over keys in m. The ordering will be oldest to newest, based on when a key was first set.

Example
package main

import (
	"fmt"

	"github.com/mroth/orderedmap"
)

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

	for k := range m.Keys() {
		fmt.Println(k)
	}
}
Output:
foo
bar
baz

func (*OrderedMap[K, V]) Len

func (m *OrderedMap[K, V]) Len() int

Len returns the length of the ordered map.

func (*OrderedMap[K, V]) Set

func (m *OrderedMap[K, V]) Set(key K, value V)

Set sets the value for a key.

func (*OrderedMap[K, V]) Values added in v0.2.0

func (m *OrderedMap[K, V]) Values() iter.Seq[V]

Values returns an iterator over values in m. The ordering will be oldest to newest, based on when a key was first set.

Example
package main

import (
	"fmt"

	"github.com/mroth/orderedmap"
)

func main() {
	m := orderedmap.New[string, string]()
	m.Set("foo", "uno")
	m.Set("bar", "dos")
	m.Set("baz", "tres")

	for v := range m.Values() {
		fmt.Println(v)
	}
}
Output:
uno
dos
tres

Jump to

Keyboard shortcuts

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