mapx

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: MIT Imports: 6 Imported by: 2

README

mapx

A generic, insertion-ordered map for Go with first-class JSON support.

mapx provides OrderedMap[K, V] — a hash map backed by a doubly linked list that preserves the order in which keys were inserted. It marshals to and from JSON with key order intact, making it ideal for APIs, config files, and anywhere deterministic output matters.

Features

  • Generics — works with any comparable key and any value type.
  • Insertion-order iterationKeys(), Values(), Range() all follow insertion order.
  • O(1) operationsGet, Set, Has, and Delete are all constant-time.
  • JSON round-trip — implements json.Marshaler and json.Unmarshaler; key order is preserved in both directions.
  • Custom key types — supports encoding.TextMarshaler / TextUnmarshaler for JSON map keys, plus all integer and string kinds.
  • Zero dependencies — only the Go standard library.

Installation

go get github.com/arisu-archive/mapx

Requires Go 1.25+.

Usage

package main

import (
	"encoding/json"
	"fmt"

	"github.com/arisu-archive/mapx"
)

func main() {
	m := mapx.New[string, int]()
	m.Set("cherry", 3)
	m.Set("apple", 1)
	m.Set("banana", 2)

	// Iteration follows insertion order.
	m.Range(func(k string, v int) bool {
		fmt.Printf("%s = %d\n", k, v)
		return true
	})
	// cherry = 3
	// apple = 1
	// banana = 2

	// JSON output preserves order.
	b, _ := json.Marshal(m)
	fmt.Println(string(b))
	// {"cherry":3,"apple":1,"banana":2}

	// Round-trip back.
	m2 := mapx.New[string, int]()
	_ = json.Unmarshal(b, m2)
	fmt.Println(m2.Keys()) // [cherry apple banana]
}

License

See LICENSE for details.

Documentation

Index

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 keeps insertion order for iteration and JSON marshalling. Existing keys updated via Set keep their original position.

func New

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

New returns an empty ordered map.

func (*OrderedMap[K, V]) Clear

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

Clear removes all entries.

func (*OrderedMap[K, V]) Delete

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

Delete removes k if present. Returns true if removed.

func (*OrderedMap[K, V]) Get

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

Get returns the value for k.

func (*OrderedMap[K, V]) Has

func (om *OrderedMap[K, V]) Has(k K) bool

Has reports whether k exists.

func (*OrderedMap[K, V]) IsZero added in v1.1.0

func (om *OrderedMap[K, V]) IsZero() bool

IsZero reports whether the map contains no entries. This enables encoding/json to respect omitempty and omitzero struct tags.

func (*OrderedMap[K, V]) Keys

func (om *OrderedMap[K, V]) Keys() []K

Keys returns keys in insertion order.

func (*OrderedMap[K, V]) Len

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

Len returns the number of items.

func (*OrderedMap[K, V]) MarshalJSON

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

func (*OrderedMap[K, V]) Range

func (om *OrderedMap[K, V]) Range(f func(k K, v V) bool)

Range iterates in insertion order; stop when f returns false.

func (*OrderedMap[K, V]) Set

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

Set inserts k with value v preserving insertion order. If k exists, only its value is updated.

func (*OrderedMap[K, V]) ToMap

func (om *OrderedMap[K, V]) ToMap() map[K]V

ToMap copies to a regular map (order lost).

func (*OrderedMap[K, V]) UnmarshalJSON

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

func (*OrderedMap[K, V]) Values

func (om *OrderedMap[K, V]) Values() []V

Values returns values in insertion order.

Jump to

Keyboard shortcuts

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