maps

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: MIT Imports: 2 Imported by: 0

README

maps

Go utilities for working with maps.

Requires Go 1.25+

Functions

Inverse

Swaps keys and values in a map. Both keys and values must be comparable.

m := map[string]int{"a": 1, "b": 2}
inverted := maps.Inverse(m) // map[int]string{1: "a", 2: "b"}
Keys (Deprecated)

Deprecated: Use slices.Collect(maps.Keys(m)) from the standard library instead.

Retrieves keys from a map as a slice. The order of keys is not guaranteed.

// Old way (deprecated)
keys := maps.Keys(m)

// New way (recommended)
keys := slices.Collect(maps.Keys(m))
Values (Deprecated)

Deprecated: Use slices.Collect(maps.Values(m)) from the standard library instead.

Retrieves values from a map as a slice. The order of values is not guaranteed.

// Old way (deprecated)
values := maps.Values(m)

// New way (recommended)
values := slices.Collect(maps.Values(m))

Migration Guide

Go 1.25+ includes a standard library maps package with iterator-based Keys() and Values() functions. To migrate:

  1. Import both maps and slices from the standard library
  2. Replace maps.Keys(m) with slices.Collect(maps.Keys(m))
  3. Replace maps.Values(m) with slices.Collect(maps.Values(m))

The Keys and Values functions in this package will be removed in a future version.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Inverse

func Inverse[K comparable, V comparable](in map[K]V) map[V]K

func Keys deprecated

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

Keys returns all keys from the map as a slice.

Deprecated: Use slices.Collect(maps.Keys(m)) from the standard library instead. This function will be removed in a future version.

Example (StandardLibrary)

Example showing how to use the standard library maps.Keys with slices.Collect. This is the recommended approach going forward.

m := map[int]string{1: "one", 2: "two", 3: "three"}

// New way: use standard library
keys := slices.Collect(stdmaps.Keys(m))
slices.Sort(keys)

// Output can't be guaranteed due to map iteration order,
// but this demonstrates the pattern
_ = keys

func Values deprecated

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

Values returns all values from the map as a slice.

Deprecated: Use slices.Collect(maps.Values(m)) from the standard library instead. This function will be removed in a future version.

Example (StandardLibrary)

Example showing how to use the standard library maps.Values with slices.Collect. This is the recommended approach going forward.

m := map[int]string{1: "one", 2: "two", 3: "three"}

// New way: use standard library
values := slices.Collect(stdmaps.Values(m))
slices.Sort(values)

// Output can't be guaranteed due to map iteration order,
// but this demonstrates the pattern
_ = values

Types

This section is empty.

Jump to

Keyboard shortcuts

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