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

mapx

package
v0.7.4 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package mapx provides generic helpers for creating, cloning, merging, and querying maps and set-maps.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendOnce

func AppendOnce[K comparable, V any](key K, value V, m map[K][]V, seen map[K]struct{})

AppendOnce appends value to m[key] only if key has not been seen yet, preventing duplicate entries for the same key across multiple sources

func AppendUnique

func AppendUnique[K, V comparable](m map[K][]V, key K, value V)

AppendUnique appends value to m[key], deduplicating by value so the slice for a key never contains the same value twice

func CloneMapStringSlice

func CloneMapStringSlice(src map[string][]string) map[string][]string

CloneMapStringSlice clones a map[string][]string, preserving the nil-vs-empty distinction of both the map and its value slices so explicit-empty markers survive the copy

func DeepCloneMapAny

func DeepCloneMapAny(src map[string]any) map[string]any

DeepCloneMapAny creates a deep copy of a map[string]any

func DeepMergeMapAny

func DeepMergeMapAny(base, override map[string]any) map[string]any

DeepMergeMapAny deep-merges override onto base, recursing into nested map[string]any values

func GetOrInit

func GetOrInit[K comparable, V any](m map[K]*V, key K, init func() *V) *V

GetOrInit returns m[key] if it is non-nil, otherwise calls init, stores the result under key, and returns it

func MapIntersectionUnique

func MapIntersectionUnique[T comparable](left, right []T) []T

MapIntersectionUnique returns items present in both slices, in right-hand order, with duplicates removed

func MapSetFromSlice

func MapSetFromSlice[T comparable](items []T) map[T]struct{}

MapSetFromSlice converts a slice into a set represented as map[T]struct{}

func Maps

func Maps(m map[string]any, key string) []map[string]any

Maps returns the objects in the list at key, read from either []any or []map[string]any; the returned maps are the stored ones, so changes to them land in m

func PruneMapZeroAny

func PruneMapZeroAny(src map[string]any) map[string]any

PruneMapZeroAny removes zero-value leaves from a nested map[string]any

func SortedProjection

func SortedProjection[K comparable, V any, T any, S cmp.Ordered](m map[K]V, project func(V) T, sortKey func(T) S) []T

SortedProjection projects map values into a target type and returns them sorted by sortKey

func SortedValues

func SortedValues[K comparable, V any, S cmp.Ordered](m map[K]V, sortKey func(V) S) []V

SortedValues returns map values sorted by a key derived from each value

func Strings

func Strings(m map[string]any, key string) []string

Strings returns the non-empty strings in the list at key, read from either []any or []string

func Value

func Value[T any](m map[string]any, key string) T

Value returns the value at key as T, the zero value when it is missing or another type

func ValueAtPath

func ValueAtPath(data map[string]any, path string) (any, bool)

ValueAtPath reads a nested value from a decoded JSON map using dotted-path notation ("outer.inner"), returning false when any segment is missing or not an object

Types

This section is empty.

Jump to

Keyboard shortcuts

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