maps

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: Apache-2.0 Imports: 1 Imported by: 1

README

Maps

The maps package provides different utility functions and data structures for working with maps in Go.

Usage

Import the package

import "go.alis.build/utils/maps"

Features

OrderedMap

The OrderedMap type is a map that maintains the order of keys. It is useful when you need to maintain the order of keys in a map.

Create a new OrderedMap instance using NewOrderedMap

    orderedMap := maps.NewOrderedMap[string, int]()
	
	orderedMap.Set("a", 1)
	orderedMap.Set("b", 2)
	
	// Get the value for a key
	a,ok := orderedMap.Get("a")
	
	// Get the keys in order
	orderedMap.Range(func(idx int, key string, value int) bool {
        fmt.Println(idx)
        fmt.Println(key)
        fmt.Println(value)
        return true
    })
	

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 structure that maintains the insertion order

func NewOrderedMap

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

NewOrderedMap creates a new instance of OrderedMap

To ensure concurrency safety and avoid race conditions and deadlocks, avoid cross-instance dependencies. Ensure that operations on different OrderedMap instances are independent and do not inadvertently depend on each other. Take the following example:

var o1, o2 OrderedMap[int, string]
go func() {
	o1.Set(1, "one")
	o2.Get(1)  // Waits on o1 indirectly
}()

go func() {
	o2.Set(2, "two")
	o1.Get(2)  // Waits on o2 indirectly
}()

One should also be careful when using the Range method. The callback function should not modify the OrderedMap.

func (*OrderedMap[K, V]) Clear

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

Clear removes all key-value pairs from the OrderedMap

func (*OrderedMap[K, V]) Delete

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

Delete removes a key-value pair from the OrderedMap

func (*OrderedMap[K, V]) Get

func (o *OrderedMap[K, V]) Get(key K) (V, bool)

Get retrieves the value associated with a key

func (*OrderedMap[K, V]) Keys

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

Keys returns the keys in the order they were added

func (*OrderedMap[K, V]) Len

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

Len returns the number of key-value pairs in the OrderedMap

func (*OrderedMap[K, V]) Range

func (o *OrderedMap[K, V]) Range(cb func(int, K, V) bool)

Range iterates over the OrderedMap in the order of insertion and applies a callback function. If the callback function returns false, the iteration stops.

The callback function should not modify the OrderedMap in any way. For example: Calling Set, Delete, or Clear inside the callback function will cause a deadlock.

func (*OrderedMap[K, V]) Set

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

Set adds or updates a key-value pair in the OrderedMap

func (*OrderedMap[K, V]) Values

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

Values returns the values in the order they were added

Jump to

Keyboard shortcuts

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