maps

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2025 License: MIT Imports: 1 Imported by: 4

Documentation

Overview

Package maps provides a set of generic functions for common operations on maps.

This package is a generated adapter that mirrors the public API of the standard Go experimental package `golang.org/x/exp/maps`. It offers a convenient way to access common utilities for cloning, extracting keys/values, and comparing maps.

For detailed information on the behavior of specific functions, please refer to the official Go documentation for the `golang.org/x/exp/maps` package.

Package maps implements the functions, types, and interfaces for the module.

Package maps contains generated code by adptool.

Package maps implements the functions, types, and interfaces for the module.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clear

func Clear[M ~map[K]V, K comparable, V any](m M)

func Clone

func Clone[M ~map[K]V, K comparable, V any](m M) M

func Concat added in v0.7.0

func Concat[M ~map[K]V, K comparable, V any](m M, ms ...M)

Concat merges multiple maps into a single map. If a key exists in multiple maps, the value from the last map will be used.

func ConcatWith added in v0.7.0

func ConcatWith[M ~map[K]V, K comparable, V any](merge func(K, V, V) V, m M, ms ...M)

ConcatWith merges multiple maps into a single map using a custom merge function. If a key exists in multiple maps, the merge function will be called to determine the final value.

func Copy

func Copy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2)

func DeleteFunc

func DeleteFunc[M ~map[K]V, K comparable, V any](m M, del func(K, V) bool)

func Equal

func Equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool

func EqualFunc

func EqualFunc[M1 ~map[K]V1, M2 ~map[K]V2, K comparable, V1, V2 any](m1 M1, m2 M2, eq func(V1, V2) bool) bool

func Exclude added in v0.7.0

func Exclude[M ~map[K]V, K comparable, V any](m M, keys ...K)

Exclude removes the specified keys from the map m.

func Filter

func Filter[M ~map[K]V, K comparable, V any](m M, f func(K, V) bool)

Filter keeps only the key-value pairs in the map for which the provided function returns true.

func FirstKey added in v0.12.0

func FirstKey[K comparable, V any](m map[K]V, keys ...K) (K, bool)

FirstKey searches for the first key from the provided list that exists in the given map. It returns the found key and true if a key is found, otherwise it returns the zero value of K and false.

Parameters:

m: The map to search within.
keys: A variadic list of keys to look for in the map.

Returns:

The first key from the 'keys' list that exists in 'm', and true.
If no key from 'keys' exists in 'm', it returns the zero value of K and false.

func FirstKeyBy added in v0.12.0

func FirstKeyBy[ListK any, MapK comparable, V any](m map[MapK]V, transform func(ListK) MapK, keys ...ListK) (ListK, bool)

FirstKeyBy searches for the first key from the provided list that, after being transformed by the 'transform' function, exists in the given map. It returns the original key from the list and true if a key is found, otherwise it returns the zero value of ListK and false. This is useful when the keys in the list need a special comparison or normalization before being looked up in the map (e.g., case-insensitive string comparison).

Parameters:

m: The map to search within. The keys of this map are of type MapK.
transform: A function that converts a key of type ListK to a key of type MapK.
keys: A variadic list of keys (of type ListK) to look for in the map after transformation.

Returns:

The first key from the 'keys' list (of type ListK) whose transformed value exists in 'm', and true.
If no transformed key from 'keys' exists in 'm', it returns the zero value of ListK and false.

func FirstKeyOrRandom added in v0.12.0

func FirstKeyOrRandom[K comparable, V any](m map[K]V, keys ...K) K

FirstKeyOrRandom searches for the first key from a list of keys that exists in the map. If a key is found, it returns that key. If no key from the list is found in the map, it returns a random key from the map. If the map is empty, it returns the zero value for the key type.

func FirstValue added in v0.12.0

func FirstValue[K comparable, V any](m map[K]V, keys ...K) (V, bool)

FirstValue searches for the first value from the provided list that exists in the given map. It returns the found value and true if a value is found, otherwise it returns the zero value of V and false.

Parameters:

m: The map to search within.
keys: A variadic list of keys to look for in the map.

Returns:

The first value from the 'keys' list that exists in 'm', and true.
If no value from 'keys' exists in 'm', it returns the zero value of V and false.

func FirstValueBy added in v0.12.0

func FirstValueBy[ListK any, MapK comparable, V any](m map[MapK]V, transform func(ListK) MapK, keys ...ListK) (V, bool)

FirstValueBy searches for the first value from the provided list that, after being transformed by the 'transform' function, exists in the given map. It returns the found value and true if a value is found, otherwise it returns the zero value of V and false. after being transformed by the 'transform' function.

Parameters:

m: The map to search within. The keys of this map are of type MapK.
transform: A function that converts a key of type ListK to a key of type MapK.
keys: A variadic list of keys (of type ListK) to look for in the map after transformation.

Returns:

The first value from the 'keys' list (of type ListK) whose transformed value exists in 'm', and true.
If no transformed value from 'keys' exists in 'm', it returns the zero value of V and false.

func FirstValueOrRandom added in v0.12.0

func FirstValueOrRandom[K comparable, V any](m map[K]V, keys ...K) V

FirstValueOrRandom searches for the value of the first key from a list that exists in the map. If a key is found, it returns its corresponding value. If no key from the list is found in the map, it returns a random value from the map. If the map is empty, it returns the zero value for the value type.

func FromKVs added in v0.7.0

func FromKVs[K comparable, V any](kvs ...KeyValue[K, V]) map[K]V

FromKVs converts a slice of key-value pairs to a map.

func FromSlice added in v0.7.0

func FromSlice[T any, K comparable, V any](ts []T, f func(T) (K, V)) map[K]V

FromSlice converts a slice of types to a map.

func FromSliceWithIndex added in v0.8.0

func FromSliceWithIndex[T any, K comparable, V any](ts []T, f func(int, T) (K, V)) map[K]V

FromSliceWithIndex converts a slice of types to a map, using the provided function to extract the key and value.

func Keys

func Keys[M ~map[K]V, K comparable, V any](m M) []K

func Merge

func Merge[M ~map[K]V, K comparable, V any](dest M, src M, overlay bool)

Merge merges the values of src into dest. If overlay is true, existing values in dest will be overwritten.

func MergeWith added in v0.7.0

func MergeWith[M ~map[K]V, K comparable, V any](dest M, src M, merge func(key K, destVal, srcVal V) V)

MergeWith merges the values of src into dest using the provided merge function. If a key exists in both maps, the merge function will be called to determine the final value.

func Random added in v0.12.0

func Random[K comparable, V any](m map[K]V) (K, V, bool)

Random returns a random key-value pair from the map. It leverages the random iteration order of maps in Go. If the map is empty, it returns the zero values for the key and value, and false.

Returns:

A random key, its corresponding value, and true if the map is not empty.
The zero values for the key and value, and false if the map is empty.

func RandomKey added in v0.12.0

func RandomKey[K comparable, V any](m map[K]V) (K, bool)

RandomKey returns a random key from the map. It leverages the random iteration order of maps in Go. If the map is empty, it returns the zero value for the key type and false.

Returns:

A random key and true if the map is not empty.
The zero value for the key type and false if the map is empty.

func RandomValue added in v0.12.0

func RandomValue[K comparable, V any](m map[K]V) (V, bool)

RandomValue returns a random value from the map. It leverages the random iteration order of maps in Go. If the map is empty, it returns the zero value for the value type and false.

Returns:

A random value and true if the map is not empty.
The zero value for the value type and false if the map is empty.

func ToSlice added in v0.7.0

func ToSlice[M ~map[K]V, K comparable, V any, T any](m M, f func(K, V) T) []T

ToSlice converts a map to a slice of types.

func ToSliceWith added in v0.8.0

func ToSliceWith[M ~map[K]V, K comparable, V any, T any](m M, f func(K, V) (T, bool)) []T

ToSliceWith converts a map to a slice of types, filtering out values that return false.

func ToStruct added in v0.7.0

func ToStruct[M ~map[K]V, K comparable, V any, S any](m M, f func(*S, K, V) *S) *S

ToStruct converts a map to a struct.

func Transform added in v0.1.5

func Transform[M ~map[K]V, K comparable, V any, TK comparable, TV any](m M, f func(K, V) (TK, TV, bool)) map[TK]TV

Transform remaps the keys and values of a map using a custom transformation function. The transformation function is called for each key-value pair in the original map. If the transformation function returns false as its third return value, the key-value pair is skipped. Otherwise, the transformed key-value pair is added to the new map.

func Values

func Values[M ~map[K]V, K comparable, V any](m M) []V

Types

type KeyValue

type KeyValue[K comparable, V any] struct {
	Key K
	Val V
}

KeyValue is a key-value pair.

func FirstEntry added in v0.14.0

func FirstEntry[K comparable, V any](m map[K]V, keys ...K) (KeyValue[K, V], bool)

FirstEntry searches for the first key from the provided list that exists in the given map. It returns the found key-value pair and true if a key is found, otherwise it returns zero values and false.

Parameters:

m: The map to search within.
keys: A variadic list of keys to look for in the map.

Returns:

A KeyValue containing the first key from 'keys' that exists in 'm' and its corresponding value, and true.
If no key from 'keys' exists in 'm', it returns zero values and false.

func FirstEntryBy added in v0.14.0

func FirstEntryBy[ListK comparable, MapK comparable, V any](m map[MapK]V, transform func(ListK) MapK, keys ...ListK) (KeyValue[ListK, V], bool)

FirstEntryBy searches for the first key from the provided list that, after being transformed by the 'transform' function, exists in the given map. It returns the original key from the list, the corresponding value from the map, and true if found. If no matching key is found, it returns zero values and false.

This is useful when the keys in the list need a special comparison or normalization before being looked up in the map (e.g., case-insensitive string comparison).

Parameters:

m: The map to search within. The keys of this map are of type MapK.
transform: A function that converts a key of type ListK to a key of type MapK.
keys: A variadic list of keys (of type ListK) to look for in the map after transformation.

Returns:

A KeyValue containing:
- Key: The original key from the 'keys' list (of type ListK)
- Val: The corresponding value from the map (of type V)
- A boolean indicating if a match was found

If no transformed key from 'keys' exists in 'm', it returns zero values and false.

func KV added in v0.10.0

func KV[K comparable, V any](key K, value V) KeyValue[K, V]

KV creates a new KeyValue pair with type inference. This is a convenience function that allows type inference when creating a KeyValue. Example:

kv := KV("a", 1) // returns KeyValue[string, int]{Key: "a", Val: 1}

func KVs added in v0.10.0

func KVs[K comparable, V any](kvs ...KeyValue[K, V]) []KeyValue[K, V]

KVs creates a slice of KeyValue pairs from the given key-value pairs. This is a convenience function that allows creating slices of KeyValue with type inference. Example:

kvs := KVs(
  KV(1, "one"),
  KV(2, "two"),
) // returns []KeyValue[int, string]

func ToKVs added in v0.7.0

func ToKVs[M ~map[K]V, K comparable, V any](m M) []KeyValue[K, V]

ToKVs converts a map to a slice of key-value pairs.

Jump to

Keyboard shortcuts

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