Generic map utilities for Go. Filter, transform, merge, group, and more
go get github.com/philiprehberger/go-maputilGeneric map utilities for Go. Filter, transform, merge, group, and more
go get github.com/philiprehberger/go-maputil
import "github.com/philiprehberger/go-maputil"
scores := map[string]int{"alice": 90, "bob": 45, "charlie": 72}
// Keep entries where value > 50
high := maputil.Filter(scores, func(_ string, v int) bool {
return v > 50
})
// {"alice": 90, "charlie": 72}
// Double all values
doubled := maputil.Map(scores, func(_ string, v int) int {
return v * 2
})
// {"alice": 180, "bob": 90, "charlie": 144}
defaults := map[string]int{"timeout": 30, "retries": 3}
overrides := map[string]int{"timeout": 60}
config := maputil.Merge(defaults, overrides)
// {"timeout": 60, "retries": 3}
// Merge with conflict resolution
summed := maputil.MergeWith(func(_ string, a, b int) int {
return a + b
}, defaults, overrides)
// {"timeout": 90, "retries": 3}
m := map[string]int{"a": 1, "b": 2, "c": 3, "d": 4}
picked := maputil.Pick(m, "a", "c")
// {"a": 1, "c": 3}
omitted := maputil.Omit(m, "b", "d")
// {"a": 1, "c": 3}
scores := map[string]int{"alice": 90, "bob": 45, "charlie": 72}
hasHigh := maputil.Any(scores, func(_ string, v int) bool {
return v >= 90
})
// true
allPassing := maputil.All(scores, func(_ string, v int) bool {
return v >= 50
})
// false (bob has 45)
config := map[string]int{"timeout": 30, "retries": 3}
timeout := maputil.GetOrDefault(config, "timeout", 60)
// 30 (key exists)
maxConns := maputil.GetOrDefault(config, "max_conns", 10)
// 10 (key missing, returns fallback)
scores := map[string]int{"alice": 90, "bob": 45, "charlie": 72}
name, score, ok := maputil.Find(scores, func(_ string, v int) bool {
return v >= 90
})
// name="alice", score=90, ok=true
scores := map[string]int{"alice": 90, "bob": 45, "charlie": 72}
passing, failing := maputil.Partition(scores, func(_ string, v int) bool {
return v >= 50
})
// passing: {"alice": 90, "charlie": 72}
// failing: {"bob": 45}
v1 := map[string]int{"a": 1, "b": 2, "c": 3}
v2 := map[string]int{"b": 2, "c": 30, "d": 4}
added, removed, changed := maputil.Diff(v1, v2)
// added: {"d": 4}
// removed: {"a": 1}
// changed: {"c": 30}
type User struct {
Name string
Department string
}
users := []User{
{"Alice", "Engineering"},
{"Bob", "Marketing"},
{"Charlie", "Engineering"},
}
grouped := maputil.GroupBy(users, func(u User) string {
return u.Department
})
// {"Engineering": [{Alice, Engineering}, {Charlie, Engineering}], "Marketing": [{Bob, Marketing}]}
counts := maputil.CountBy(users, func(u User) string {
return u.Department
})
// {"Engineering": 2, "Marketing": 1}
| Function | Description |
|---|---|
Filter | Return map with entries matching predicate |
Map | Transform values |
MapKeys | Transform keys |
Merge | Merge maps, last wins |
MergeWith | Merge with conflict resolution |
Pick | Select only given keys |
Omit | Exclude given keys |
Invert | Swap keys and values |
Keys | Extract keys (unordered) |
SortedKeys | Extract keys sorted |
Values | Extract values |
Contains | Check if key exists |
Size | Return map size |
Any | True if any entry matches predicate |
All | True if all entries match predicate |
GetOrDefault | Get value or return fallback |
Find | First entry matching predicate |
Partition | Split map by predicate |
Diff | Compare two maps (added, removed, changed) |
GroupBy | Group slice elements by key |
CountBy | Count elements per group |
UniqueBy | Last element per group |
go test ./...
go vet ./...
If you find this project useful: