omap

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2025 License: ISC Imports: 7 Imported by: 0

README

Golang Ordered Map

Go Reference

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-omap

Usage

package main

import (
    omap "github.com/nicolasparada/go-omap"
)

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 omap.Map[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}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

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

Map is an ordered map that maintains insertion order of keys while providing O(1) key access through an internal map.

func New

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

New creates a new ordered map.

func (*Map[K, V]) All added in v0.5.0

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

All returns an iterator over key-value pairs in insertion order. This allows using the map with range loops:

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

func (*Map[K, V]) Delete

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

Delete removes the key-value pair from the map.

func (*Map[K, V]) Get

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

Get returns the value for the given key and true if it exists, or the zero value and false if it doesn't.

func (*Map[K, V]) Has

func (om *Map[K, V]) Has(key K) bool

Has returns true if the key exists in the map.

func (*Map[K, V]) Keys added in v0.5.0

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

Keys returns a slice of all keys in insertion order.

func (*Map[K, V]) Len added in v0.5.0

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

Len returns the number of key-value pairs in the map.

func (*Map[K, V]) MarshalJSON

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

MarshalJSON implements json.Marshaler interface to marshal an ordered map into a JSON object.

func (*Map[K, V]) MarshalYAML

func (om *Map[K, V]) MarshalYAML() (any, error)

MarshalYAML implements yaml.Marshaler interface to marshal an ordered map into a YAML object.

func (*Map[K, V]) Set

func (om *Map[K, V]) Set(key K, val V)

Set sets the value for the given key. If the key already exists, it updates the value without changing the order. If the key is new, it appends the key to the end.

func (*Map[K, V]) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler interface to unmarshal a JSON object into an ordered map. Nested objects are automatically converted to ordered maps.

func (*Map[K, V]) UnmarshalYAML

func (om *Map[K, V]) UnmarshalYAML(node *yaml.Node) error

UnmarshalYAML implements yaml.Unmarshaler interface to unmarshal a YAML object into an ordered map. Nested objects are automatically converted to ordered maps.

func (*Map[K, V]) Values added in v0.5.0

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

Values returns a slice of all values in insertion order.

Jump to

Keyboard shortcuts

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