Help us shape the future of Go! Take the 2026 Go Developer Survey

mapx

package
v1.104.2 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package mapx contains extra map types and functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RepopulateNonzero added in v1.102.0

func RepopulateNonzero[K comparable, V comparable](m *map[K]V, populate func())

RepopulateNonzero re-uses an existing map (preserving its allocated buckets) by zeroing all values, calling populate to re-fill it, and then deleting any entries still at their zero value. If *m is nil it is lazily initialized.

This avoids the allocation cost of creating a new map on every call, which matters for maps that are rebuilt frequently (e.g. on every netmap update).

Types

type OrderedMap

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

OrderedMap is a map that maintains the order of its keys.

It is meant for maps that only grow or that are small; is it not optimized for deleting keys.

The zero value is ready to use.

Locking-wise, it has the same rules as a regular Go map: concurrent reads are safe, but not writes.

func (*OrderedMap[K, V]) All

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

All yields all the keys and values, in the order they were inserted.

func (*OrderedMap[K, V]) Contains

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

Contains reports whether the map contains the given key.

func (*OrderedMap[K, V]) Delete

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

Delete removes the key from the map.

The cost is O(n) in the number of keys in the map.

func (*OrderedMap[K, V]) Get

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

Get returns the value for the given key in the map. If the key does not exist, it returns the zero value for V.

func (*OrderedMap[K, V]) GetOk

func (m *OrderedMap[K, V]) GetOk(key K) (_ V, ok bool)

GetOk returns the value for the given key in the map and whether it was present in the map.

func (*OrderedMap[K, V]) Keys

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

Keys yields the map keys, in the order they were inserted.

func (*OrderedMap[K, V]) Set

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

Set sets the value for the given key in the map.

If the key already exists, it updates the value and keeps the order.

func (*OrderedMap[K, V]) Values

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

Values yields the map values, in the order they were inserted.

Jump to

Keyboard shortcuts

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