Documentation
¶
Overview ¶
Package maps contains functions related to maps such as deep copying and equality testing.
Index ¶
- func CopyMap(m map[string]any) map[string]any
- func Difference[K comparable, V any](m map[K]V, n map[K]V)
- func DifferenceNew[K comparable, V any](m map[K]V, n map[K]V) map[K]V
- func Filter[K comparable, V any](m map[K]V, fun func(i int, key K, val V) bool)
- func FilterNew[K comparable, V any](m map[K]V, fun func(i int, key K, val V) bool) map[K]V
- func JsonMapEqualTest(t *testing.T, actual, expected any, forString string)
- func Keys[K comparable, V any](m map[K]V) []K
- func OrderedKeys[K constraints.Ordered, V any](m map[K]V) []K
- func RangeKeys[K comparable, V any](m map[K]V, fun MapRangeFunc[K, V])
- func RangeOrderedKeys[K constraints.Ordered, V any](m map[K]V, fun MapRangeFunc[K, V])
- func Union[K comparable, V any](dst map[K]V, src map[K]V)
- func UnionNew[K comparable, V any](m map[K]V, n map[K]V) map[K]V
- func Values[K comparable, V any](m map[K]V) []V
- type MapRangeFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CopyMap ¶
CopyMap clones a map deeply using recursion.
Example ¶
Deep copying a map using recursion via CopyMap.
CopyMap was designed for use with deserialised JSONs hence the map[string]any signature.
original := map[string]any{
"hello": "world",
"age": 20,
"bald": false,
"friends": []any{
"Bob",
"Jane",
"John",
"Mark",
map[string]any{
"name": "Gregor",
"age": 31,
"friends": []any{
"Bill",
"Bob",
"Sarah",
},
},
},
}
// Clone the above map.
clone := CopyMap(original)
fmt.Println("Original:", original)
fmt.Println("Clone:", clone)
Output: Original: map[age:20 bald:false friends:[Bob Jane John Mark map[age:31 friends:[Bill Bob Sarah] name:Gregor]] hello:world] Clone: map[age:20 bald:false friends:[Bob Jane John Mark map[age:31 friends:[Bill Bob Sarah] name:Gregor]] hello:world]
func Difference ¶ added in v2.3.5
func Difference[K comparable, V any](m map[K]V, n map[K]V)
Difference removes every key-value pair in m that also exists in n.
Example ¶
Find the difference of two maps, storing the difference in the first map.
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
n := map[string]int{
"a": 4,
"b": 5,
"d": 6,
}
Difference(m, n)
fmt.Println(m)
Output: map[c:3]
func DifferenceNew ¶ added in v2.4.0
func DifferenceNew[K comparable, V any](m map[K]V, n map[K]V) map[K]V
DifferenceNew works similarly to Difference expect it returns a new map.
Example ¶
Find the difference of two maps, storing the difference in a new map.
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
n := map[string]int{
"a": 4,
"b": 5,
"d": 6,
}
fmt.Println(DifferenceNew(m, n))
Output: map[c:3]
func Filter ¶ added in v2.3.5
func Filter[K comparable, V any](m map[K]V, fun func(i int, key K, val V) bool)
Filter takes a map and runs the given predicate function on each index-key-value triple. If the predicate returns false for an element, then that element will be removed from the given map.
Example ¶
Filter out all key-value pairs for a map of strings to integers whose values are <= 2.
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
Filter(m, func(i int, key string, val int) bool {
return val > 2
})
fmt.Println(m)
Output: map[c:3]
func FilterNew ¶ added in v2.4.0
func FilterNew[K comparable, V any](m map[K]V, fun func(i int, key K, val V) bool) map[K]V
FilterNew works similarly to Filter except it returns a new map.
Example ¶
Filter out all key-value pairs for a map of strings to integers whose values are <= 2.
Notice how we print the returned map.
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
fmt.Println(FilterNew(m, func(i int, key string, val int) bool {
return val > 2
}))
Output: map[c:3]
func JsonMapEqualTest ¶
JsonMapEqualTest used in tests to check equality between two anys.
This takes into account orderings of slices.
func Keys ¶ added in v2.3.0
func Keys[K comparable, V any](m map[K]V) []K
Keys returns the keys within a given map.
Example ¶
Retrieve the keys from a map.
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
for _, key := range Keys(m) {
fmt.Println(key)
}
Output: a b c
func OrderedKeys ¶ added in v2.3.0
func OrderedKeys[K constraints.Ordered, V any](m map[K]V) []K
OrderedKeys returns the ordered keys for a given map.
Example ¶
Retrieve the keys from the map in order.
m := map[string]int{
"c": 3,
"b": 2,
"a": 1,
}
fmt.Println(OrderedKeys(m))
Output: [a b c]
func RangeKeys ¶ added in v2.3.0
func RangeKeys[K comparable, V any](m map[K]V, fun MapRangeFunc[K, V])
RangeKeys calls the given MapRangeFunc on each index-key-value triple. Triples are unordered.
Example ¶
Range over a map and display the index, key, and value of the current key-value pair.
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
RangeKeys(m, func(i int, key string, val int) bool {
fmt.Printf("%s-%d\n", key, val)
return true
})
Output: a-1 b-2 c-3
func RangeOrderedKeys ¶ added in v2.3.0
func RangeOrderedKeys[K constraints.Ordered, V any](m map[K]V, fun MapRangeFunc[K, V])
RangeOrderedKeys calls the given MapRangeFunc on each index-key-value triple. Triples are ordered by their keys.
Example ¶
Range over a map, in key order, and display the index, key, and value of the current key-value pair.
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
RangeOrderedKeys(m, func(i int, key string, val int) bool {
fmt.Printf("%d-%s-%d\n", i, key, val)
return true
})
Output: 0-a-1 1-b-2 2-c-3
func Union ¶ added in v2.3.5
func Union[K comparable, V any](dst map[K]V, src map[K]V)
Union takes merges the source map into the destination map, overriding any matching keys.
Example ¶
Union two maps together, storing the result in the first map.
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
n := map[string]int{
"c": 4,
"d": 5,
"e": 6,
}
Union(m, n)
fmt.Println(m)
Output: map[a:1 b:2 c:4 d:5 e:6]
func UnionNew ¶ added in v2.4.0
func UnionNew[K comparable, V any](m map[K]V, n map[K]V) map[K]V
UnionNew works similarly to Union expect it returns a new map.
Example ¶
Union two maps together, storing the result in a new map.
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
n := map[string]int{
"c": 4,
"d": 5,
"e": 6,
}
fmt.Println(UnionNew(m, n))
Output: map[a:1 b:2 c:4 d:5 e:6]
func Values ¶ added in v2.3.0
func Values[K comparable, V any](m map[K]V) []V
Values returns the values within a given map.
Example ¶
Retrieve the values from a map.
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
for _, value := range Values(m) {
fmt.Println(value)
}
Output: 1 2 3
Types ¶
type MapRangeFunc ¶ added in v2.3.0
type MapRangeFunc[K comparable, V any] func(i int, key K, val V) bool
MapRangeFunc is the signature passed to RangeKeys, and RangeOrderedKeys. It is passed the key-value pair, and should return whether you want to keep iterating over the map.