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

maps

package
v1.0.109 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package maps provides generic map helpers and a typed wrapper around sync.Map.

Several entry points are deprecated in favor of the standard library: Keys/Values/OrderedKeys (maps + slices, Go 1.23) and Merge (maps.Clone/Copy). SyncMap remains useful because sync.Map is untyped.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) bool

All returns true if all key-value pairs satisfy the predicate

func Any

func Any[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) bool

Any returns true if any key-value pair satisfies the predicate

func Count

func Count[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) int

Count returns the number of key-value pairs that satisfy the predicate

func Drop

func Drop[K comparable, V any](m map[K]V, n int) map[K]V

Drop returns the map with the first n key-value pairs removed

func Each

func Each[K comparable, V any](m map[K]V, f func(key K, value V))

Each applies a function to each key-value pair in the map

func Filter

func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V

Filter returns a new map containing only key-value pairs that satisfy the predicate

func Find

func Find[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) (K, V, bool)

Find returns the first key-value pair that satisfies the predicate, or false if none found

func GroupBy

func GroupBy[K comparable, V any, G comparable](m map[K]V, keyFunc func(key K, value V) G) map[G][]V

GroupBy groups values by a key function

func Invert

func Invert[K comparable, V comparable](m map[K]V) map[V][]K

Invert swaps keys and values (values must be comparable)

func Keys deprecated

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

Keys returns all keys from the map as a slice.

Deprecated: use slices.Collect(maps.Keys(m)) from the standard library (Go 1.23+). The standard-library expression returns nil for an empty map; Keys returns a non-nil empty slice.

func Map

func Map[K comparable, V any, R any](m map[K]V, f func(key K, value V) R) map[K]R

Map transforms each value in the map using the provided function

func Max

func Max[K comparable, V any](m map[K]V, less func(a, b V) bool) (K, V, bool)

Max returns the key-value pair with the maximum value according to the comparison function

func Merge

func Merge[K comparable, V any](maps ...map[K]V) map[K]V

Merge combines multiple maps into one, with later maps taking precedence.

use maps.Clone and maps.Copy from the standard library (Go 1.21+). Merge always returns a non-nil map, including with no inputs.

func Min

func Min[K comparable, V any](m map[K]V, less func(a, b V) bool) (K, V, bool)

Min returns the key-value pair with the minimum value according to the comparison function

func OrderedKeys deprecated added in v0.16.91

func OrderedKeys[K cmp.Ordered, V any](m map[K]V) []K

OrderedKeys returns all keys from the map as a sorted slice.

Deprecated: use slices.Sorted(maps.Keys(m)) from the standard library (Go 1.23+). The standard-library expression returns nil for an empty map; OrderedKeys returns a non-nil empty slice.

func OrderedRange added in v0.16.91

func OrderedRange[K cmp.Ordered, V any](c map[K]V, f func(k K, v V) bool)

OrderedRange range over map keys in order

func Partition

func Partition[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) (map[K]V, map[K]V)

Partition splits the map into two based on the predicate

func Range added in v0.16.91

func Range[K cmp.Ordered, V any](c map[K]V, f func(k K, v V) bool)

Range range over map keys

func Reduce

func Reduce[K comparable, V any, R any](m map[K]V, initial R, f func(acc R, key K, value V) R) R

Reduce accumulates values across the map using the provided function

func Take

func Take[K comparable, V any](m map[K]V, n int) map[K]V

Take returns the first n key-value pairs from the map

func Values deprecated

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

Values returns all values from the map as a slice.

Deprecated: use slices.Collect(maps.Values(m)) from the standard library (Go 1.23+). The standard-library expression returns nil for an empty map; Values returns a non-nil empty slice.

Types

type SyncMap

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

SyncMap is a typed wrapper around sync.Map.

func (*SyncMap[K, V]) Delete

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

func (*SyncMap[K, V]) Load

func (m *SyncMap[K, V]) Load(key K) (value V, ok bool)

func (*SyncMap[K, V]) LoadAndDelete

func (m *SyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool)

func (*SyncMap[K, V]) LoadOrStore

func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

func (*SyncMap[K, V]) Range

func (m *SyncMap[K, V]) Range(f func(key K, value V) bool)

func (*SyncMap[K, V]) Store

func (m *SyncMap[K, V]) Store(key K, value V)

Jump to

Keyboard shortcuts

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