orderedmap

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 5 Imported by: 0

README

orderedmap

GitHub tag (latest by date) Go Reference CI codecov Go Report Card

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

Installation

go get github.com/winebarrel/orderedmap

Requires Go 1.23+.

Usage

package main

import (
	"fmt"

	"github.com/winebarrel/orderedmap"
)

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.Pairs()
	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()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map added in v1.1.0

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

func New

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

func (*Map[K, V]) All added in v1.1.0

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

func (*Map[K, V]) Clear added in v1.1.0

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

func (*Map[K, V]) Delete added in v1.1.0

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

func (*Map[K, V]) DeleteOk added in v1.1.0

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

func (*Map[K, V]) Get added in v1.1.0

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

func (*Map[K, V]) GetOk added in v1.1.0

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

func (*Map[K, V]) Keys added in v1.1.0

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

func (*Map[K, V]) Len added in v1.1.0

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

func (*Map[K, V]) Pairs added in v1.1.0

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

func (*Map[K, V]) Push added in v1.1.0

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

func (*Map[K, V]) Set added in v1.1.0

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

func (*Map[K, V]) String added in v1.1.0

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

func (*Map[K, V]) Values added in v1.1.0

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