This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.
Your feedback helps us improve!
Core - Map helpersโ
This page lists all operations on maps, available in the core package of lo.
Creates a slice of the map keys.
Use the UniqKeys variant to deduplicate common keys.
keys := lo.Keys(map[string]int{"foo": 1, "bar": 2})
// []string{"foo", "bar"}
keys := lo.Keys(map[string]int{"foo": 1, "bar": 2}, map[string]int{"baz": 3})
// []string{"foo", "bar", "baz"}
keys := lo.Keys(map[string]int{"foo": 1, "bar": 2}, map[string]int{"bar": 3})
// []string{"foo", "bar", "bar"}Prototype:func Keys[K comparable, V any](in ...map[K]V) []K
UniqKeysโ
Creates a slice of unique map keys across one or more maps.
keys := lo.UniqKeys(map[string]int{"foo": 1, "bar": 2}, map[string]int{"bar": 3})
// []string{"foo", "bar"}Similar:Prototype:func UniqKeys[K comparable, V any](in ...map[K]V) []K
HasKeyโ
Returns whether the given key exists in the map.
exists := lo.HasKey(map[string]int{"foo": 1, "bar": 2}, "foo")
// true
exists = lo.HasKey(map[string]int{"foo": 1, "bar": 2}, "baz")
// falsePrototype:func HasKey[K comparable, V any](in map[K]V, key K) bool
Valuesโ
Creates a slice of the map values across one or more maps.
values := lo.Values(map[string]int{"foo": 1, "bar": 2})
// []int{1, 2}
values = lo.Values(map[string]int{"foo": 1}, map[string]int{"bar": 2})
// []int{1, 2}Prototype:func Values[K comparable, V any](in ...map[K]V) []V
UniqValuesโ
Creates a slice of unique map values across one or more maps.
values := lo.UniqValues(map[string]int{"foo": 1, "bar": 2}, map[string]int{"bar": 2})
// []int{1, 2}Prototype:func UniqValues[K comparable, V comparable](in ...map[K]V) []V
ValueOrโ
Returns the value for a key or a fallback if the key is not present.
value := lo.ValueOr(map[string]int{"foo": 1, "bar": 2}, "foo", 42)
// 1
value = lo.ValueOr(map[string]int{"foo": 1, "bar": 2}, "baz", 42)
// 42Prototype:func ValueOr[K comparable, V any](in map[K]V, key K, fallback V) V
PickByโ
Returns a map of the same type filtered by a key/value predicate.
m := lo.PickBy(
map[string]int{"foo": 1, "bar": 2, "baz": 3},
func(key string, value int) bool {
return value%2 == 1
},
)
// map[string]int{"foo": 1, "baz": 3}Prototype:func PickBy[K comparable, V any, Map ~map[K]V](in Map, predicate func(key K, value V) bool) Map
PickByKeysโ
Returns a map of the same type filtered by the provided keys.
m := lo.PickByKeys(
map[string]int{"foo": 1, "bar": 2, "baz": 3},
[]string{"foo", "baz"},
)
// map[string]int{"foo": 1, "baz": 3}Prototype:func PickByKeys[K comparable, V any, Map ~map[K]V](in Map, keys []K) Map
PickByValuesโ
Returns a map of the same type filtered by the provided values.
m := lo.PickByValues(map[string]int{"foo": 1, "bar": 2, "baz": 3}, []int{1, 3})
// map[string]int{"foo": 1, "baz": 3}Prototype:func PickByValues[K comparable, V comparable, Map ~map[K]V](in Map, values []V) Map
OmitByโ
Returns a map of the same type excluding entries that match the predicate.
m := lo.OmitBy(
map[string]int{"foo": 1, "bar": 2, "baz": 3},
func(key string, value int) bool {
return value%2 == 1
},
)
// map[string]int{"bar": 2}Prototype:func OmitBy[K comparable, V any, Map ~map[K]V](in Map, predicate func(key K, value V) bool) Map
OmitByKeysโ
Returns a map of the same type excluding the provided keys.
m := lo.OmitByKeys(map[string]int{"foo": 1, "bar": 2, "baz": 3}, []string{"foo", "baz"})
// map[string]int{"bar": 2}Prototype:func OmitByKeys[K comparable, V any, Map ~map[K]V](in Map, keys []K) Map
OmitByValuesโ
Returns a map of the same type excluding the provided values.
m := lo.OmitByValues(map[string]int{"foo": 1, "bar": 2, "baz": 3}, []int{1, 3})
// map[string]int{"bar": 2}Prototype:func OmitByValues[K comparable, V comparable, Map ~map[K]V](in Map, values []V) Map
Entriesโ
Transforms a map into a slice of key/value pairs.
entries := lo.Entries(map[string]int{"foo": 1, "bar": 2})
// []lo.Entry[string, int]{ {Key: "foo", Value: 1}, {Key: "bar", Value: 2} }Similar:Prototype:func Entries[K comparable, V any](in map[K]V) []Entry[K, V]
FromEntriesโ
Transforms a slice of key/value pairs into a map.
m := lo.FromEntries([]lo.Entry[string, int]{
{Key: "foo", Value: 1},
{Key: "bar", Value: 2},
})
// map[string]int{"foo": 1, "bar": 2}Prototype:func FromEntries[K comparable, V any](entries []Entry[K, V]) map[K]V
ToPairsโ
Transforms a map into a slice of key/value pairs. Alias of
Entries
.pairs := lo.ToPairs(map[string]int{"foo": 1, "bar": 2})
// []lo.Entry[string, int]{...}Similar:Prototype:func ToPairs[K comparable, V any](in map[K]V) []Entry[K, V]
FromPairsโ
Transforms a slice of key/value pairs into a map. Alias of
FromEntries
.m := lo.FromPairs([]lo.Entry[string, int]{{Key: "foo", Value: 1}})
// map[string]int{"foo": 1}Prototype:func FromPairs[K comparable, V any](entries []Entry[K, V]) map[K]V
Invertโ
Creates a map with keys and values swapped. If values are duplicated, later keys overwrite earlier ones.
lo.Invert(map[string]int{"a": 1, "b": 2})
// map[int]string{1: "a", 2: "b"}Prototype:func Invert[K comparable, V comparable](in map[K]V) map[V]K
Assignโ
Merges multiple maps from left to right. Later maps overwrite earlier keys.
merged := lo.Assign(
map[string]int{"a": 1, "b": 2},
map[string]int{"b": 3, "c": 4},
)
// map[string]int{"a": 1, "b": 3, "c": 4}Prototype:func Assign[K comparable, V any, Map ~map[K]V](maps ...Map) Map
ChunkEntriesโ
Splits a map into maps of at most the given size. The last chunk may be smaller.
chunks := lo.ChunkEntries(map[string]int{"a":1, "b":2, "c":3, "d":4, "e":5}, 3)
// []map[string]int{ {"a":1, "b":2, "c":3}, {"d":4, "e":5} }Prototype:func ChunkEntries[K comparable, V any](m map[K]V, size int) []map[K]V
MapKeysโ
Transforms map keys using a predicate while keeping values.
in := map[int]int{1:1, 2:2}
out := lo.MapKeys(in, func(v int, _ int) string {
return strconv.Itoa(v)
})
// map[string]int{"1":1, "2":2}Similar:Prototype:func MapKeys[K comparable, V any, R comparable](in map[K]V, iteratee func(value V, key K) R) map[R]V
MapValuesโ
Transforms map values using a predicate while keeping keys.
in := map[int]int64{1:1, 2:2}
out := lo.MapValues(in, func(v int64, _ int) string {
return strconv.FormatInt(v, 10)
})
// map[int]string{1:"1", 2:"2"}Similar:Prototype:func MapValues[K comparable, V any, R any](in map[K]V, iteratee func(value V, key K) R) map[K]R
MapEntriesโ
Transforms both keys and values using an predicate function.
in := map[string]int{"foo":1, "bar":2}
out := lo.MapEntries(in, func(k string, v int) (int, string) {
return v, k
})
// map[int]string{1:"foo", 2:"bar"}Similar:Prototype:func MapEntries[K1 comparable, V1 any, K2 comparable, V2 any](in map[K1]V1, iteratee func(key K1, value V1) (K2, V2)) map[K2]V2
MapToSliceโ
Transforms a map into a slice by applying an predicate to each key/value pair.
m := map[int]int64{1:4, 2:5, 3:6}
s := lo.MapToSlice(m, func(k int, v int64) string {
return fmt.Sprintf("%d_%d", k, v)
})
// []string{"1_4", "2_5", "3_6"}Similar:Prototype:func MapToSlice[K comparable, V any, R any](in map[K]V, iteratee func(key K, value V) R) []R
FilterMapToSliceโ
Transforms a map into a slice using an predicate that returns a value and a boolean to include it.
kv := map[int]int64{1:1, 2:2, 3:3, 4:4}
result := lo.FilterMapToSlice(kv, func(k int, v int64) (string, bool) {
return fmt.Sprintf("%d_%d", k, v), k%2 == 0
})
// []string{"2_2", "4_4"}Prototype:func FilterMapToSlice[K comparable, V any, R any](in map[K]V, iteratee func(key K, value V) (R, bool)) []R
FilterKeysโ
Returns a slice of keys for which the predicate is true.
kv := map[int]string{1:"foo", 2:"bar", 3:"baz"}
result := lo.FilterKeys(kv, func(k int, v string) bool {
return v == "foo"
})
// []int{1}Prototype:func FilterKeys[K comparable, V any](in map[K]V, predicate func(key K, value V) bool) []K
FilterValuesโ
Returns a slice of values for which the predicate is true.
kv := map[int]string{1:"foo", 2:"bar", 3:"baz"}
result := lo.FilterValues(kv, func(k int, v string) bool {
return v == "foo"
})
// []string{"foo"}Prototype:func FilterValues[K comparable, V any](in map[K]V, predicate func(key K, value V) bool) []V