Go utilities for working with maps.
Requires Go 1.25+
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"}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))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))Go 1.25+ includes a standard library maps package with iterator-based Keys() and Values() functions. To migrate:
- Import both
mapsandslicesfrom the standard library - Replace
maps.Keys(m)withslices.Collect(maps.Keys(m)) - Replace
maps.Values(m)withslices.Collect(maps.Values(m))
The Keys and Values functions in this package will be removed in a future version.