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) []KUniqKeysโ
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) []KHasKeyโ
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) boolValuesโ
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) []VUniqValuesโ
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) []VValueOrโ
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) VPickByโ
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) MapPickByErrโ
Returns a map of the same type filtered by a key/value predicate. Returns an error if the predicate function fails, stopping iteration immediately.
m, err := lo.PickByErr(
map[string]int{"foo": 1, "bar": 2, "baz": 3},
func(key string, value int) (bool, error) {
if key == "bar" {
return false, fmt.Errorf("bar not allowed")
}
return value%2 == 1, nil
},
)
// map[string]int(nil), error("bar not allowed")m, err := lo.PickByErr(
map[string]int{"foo": 1, "bar": 2, "baz": 3},
func(key string, value int) (bool, error) {
return value%2 == 1, nil
},
)
// map[string]int{"foo": 1, "baz": 3}, nilPrototype:func PickByErr[K comparable, V any, Map ~map[K]V](in Map, predicate func(key K, value V) (bool, error)) (Map, error)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) MapPickByValuesโ
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) MapOmitByโ
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) MapOmitByErrโ
Returns a map of the same type excluding entries that match the predicate. Returns an error if the predicate function fails, stopping iteration immediately.
m, err := lo.OmitByErr(
map[string]int{"foo": 1, "bar": 2, "baz": 3},
func(key string, value int) (bool, error) {
if key == "bar" {
return false, fmt.Errorf("bar not allowed")
}
return value%2 == 1, nil
},
)
// map[string]int(nil), error("bar not allowed")m, err := lo.OmitByErr(
map[string]int{"foo": 1, "bar": 2, "baz": 3},
func(key string, value int) (bool, error) {
return value%2 == 1, nil
},
)
// map[string]int{"bar": 2}, nilPrototype:func OmitByErr[K comparable, V any, Map ~map[K]V](in Map, predicate func(key K, value V) (bool, error)) (Map, error)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) MapOmitByValuesโ
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) MapEntriesโ
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]VToPairsโ
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]VInvertโ
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]KAssignโ
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) MapChunkEntriesโ
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]VMapKeysโ
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}Prototype:func MapKeys[K comparable, V any, R comparable](in map[K]V, iteratee func(value V, key K) R) map[R]VMapKeysErrโ
Transforms map keys using a predicate while keeping values. Returns an error if the iteratee function fails, stopping iteration immediately.
in := map[int]int{1: 1, 2: 2, 3: 3}
out, err := lo.MapKeysErr(in, func(v int, _ int) (string, error) {
if v == 2 {
return "", fmt.Errorf("even number not allowed")
}
return strconv.Itoa(v), nil
})
// map[string]int(nil), error("even number not allowed")in := map[int]int{1: 1, 2: 2, 3: 3}
out, err := lo.MapKeysErr(in, func(v int, _ int) (string, error) {
return strconv.Itoa(v), nil
})
// map[string]int{"1":1, "2":2, "3":3}, nilSimilar:Prototype:func MapKeysErr[K comparable, V any, R comparable](in map[K]V, iteratee func(value V, key K) (R, error)) (map[R]V, error)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"}Prototype:func MapValues[K comparable, V any, R any](in map[K]V, iteratee func(value V, key K) R) map[K]RMapValuesErrโ
Transforms map values using a predicate while keeping keys. Returns an error if the iteratee function fails, stopping iteration immediately.
in := map[int]int64{1: 1, 2: 2, 3: 3}
out, err := lo.MapValuesErr(in, func(v int64, _ int) (string, error) {
if v == 2 {
return "", fmt.Errorf("even number not allowed")
}
return strconv.FormatInt(v, 10), nil
})
// map[int]string(nil), error("even number not allowed")in := map[int]int64{1: 1, 2: 2, 3: 3}
out, err := lo.MapValuesErr(in, func(v int64, _ int) (string, error) {
return strconv.FormatInt(v, 10), nil
})
// map[int]string{1:"1", 2:"2", 3:"3"}, nilSimilar:Prototype:func MapValuesErr[K comparable, V any, R any](in map[K]V, iteratee func(value V, key K) (R, error)) (map[K]R, error)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"}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]V2MapEntriesErrโ
Transforms both keys and values using an predicate function. Returns an error if the iteratee function fails, stopping iteration immediately.
in := map[string]int{"foo": 1, "bar": 2, "baz": 3}
out, err := lo.MapEntriesErr(in, func(k string, v int) (int, string, error) {
if k == "bar" {
return 0, "", fmt.Errorf("bar not allowed")
}
return v, k, nil
})
// map[int]string(nil), error("bar not allowed")in := map[string]int{"foo": 1, "bar": 2}
out, err := lo.MapEntriesErr(in, func(k string, v int) (int, string, error) {
return v, k, nil
})
// map[int]string{1:"foo", 2:"bar"}, nilSimilar:Prototype:func MapEntriesErr[K1 comparable, V1 any, K2 comparable, V2 any](in map[K1]V1, iteratee func(key K1, value V1) (K2, V2, error)) (map[K2]V2, error)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"}Prototype:func MapToSlice[K comparable, V any, R any](in map[K]V, iteratee func(key K, value V) R) []RMapToSliceErrโ
Transforms a map into a slice by applying an predicate to each key/value pair. Returns an error if the iteratee function fails, stopping iteration immediately.
m := map[int]int64{1: 4, 2: 5, 3: 6}
s, err := lo.MapToSliceErr(m, func(k int, v int64) (string, error) {
if k == 2 {
return "", fmt.Errorf("key 2 not allowed")
}
return fmt.Sprintf("%d_%d", k, v), nil
})
// []string(nil), error("key 2 not allowed")m := map[int]int64{1:4, 2:5, 3:6}
s, err := lo.MapToSliceErr(m, func(k int, v int64) (string, error) {
return fmt.Sprintf("%d_%d", k, v), nil
})
// []string{"1_4", "2_5", "3_6"}, nilSimilar:Prototype:func MapToSliceErr[K comparable, V any, R any](in map[K]V, iteratee func(key K, value V) (R, error)) ([]R, error)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)) []RFilterMapToSliceErrโ
Transforms a map into a slice using a predicate that returns a value, a boolean to include it, and an error. Stops iteration immediately on error.
kv := map[int]int64{1:1, 2:2, 3:3, 4:4}
result, err := lo.FilterMapToSliceErr(kv, func(k int, v int64) (string, bool, error) {
if k == 3 {
return "", false, fmt.Errorf("key 3 not allowed")
}
return fmt.Sprintf("%d_%d", k, v), k%2 == 0, nil
})
// []string(nil), error("key 3 not allowed")kv := map[int]int64{1:1, 2:2, 3:3, 4:4}
result, err := lo.FilterMapToSliceErr(kv, func(k int, v int64) (string, bool, error) {
return fmt.Sprintf("%d_%d", k, v), k%2 == 0, nil
})
// []string{"2_2", "4_4"}, nilPrototype:func FilterMapToSliceErr[K comparable, V any, R any](in map[K]V, iteratee func(key K, value V) (R, bool, error)) ([]R, error)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) []KFilterKeysErrโ
Transforms a map into a slice of keys based on a predicate that can return an error. It is a mix of Filter() and Keys() with error handling. If the predicate returns true, the key is included. If the predicate returns an error, iteration stops immediately and returns the error.
kv := map[int]string{1:"foo", 2:"bar", 3:"baz"}
result, err := lo.FilterKeysErr(kv, func(k int, v string) (bool, error) {
if k == 3 {
return false, errors.New("key 3 not allowed")
}
return v == "foo", nil
})
// []int(nil), error("key 3 not allowed")kv := map[int]string{1:"foo", 2:"bar", 3:"baz"}
result, err := lo.FilterKeysErr(kv, func(k int, v string) (bool, error) {
return v == "bar", nil
})
// []int{2}, nilSimilar:Prototype:func FilterKeysErr[K comparable, V any](in map[K]V, predicate func(key K, value V) (bool, error)) ([]K, error)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) []VFilterValuesErrโ
Transforms a map into a slice of values based on a predicate that can return an error. It is a mix of Filter() and Values() with error handling. If the predicate returns true, the value is included. If the predicate returns an error, iteration stops immediately and returns the error.
kv := map[int]string{1:"foo", 2:"bar", 3:"baz"}
result, err := lo.FilterValuesErr(kv, func(k int, v string) (bool, error) {
if k == 3 {
return false, errors.New("key 3 not allowed")
}
return v == "foo", nil
})
// []string(nil), error("key 3 not allowed")kv := map[int]string{1:"foo", 2:"bar", 3:"baz"}
result, err := lo.FilterValuesErr(kv, func(k int, v string) (bool, error) {
return v == "bar", nil
})
// []string{"bar"}, nilSimilar:Prototype:func FilterValuesErr[K comparable, V any](in map[K]V, predicate func(key K, value V) (bool, error)) ([]V, error)