package maps
import "maps"
Package maps defines various functions useful with maps of any type.
Index ¶
- func Clone[M ~map[K]V, K comparable, V any](m M) M
- func Copy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2)
- func DeleteFunc[M ~map[K]V, K comparable, V any](m M, del func(K, V) bool)
- func Equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool
- func EqualFunc[M1 ~map[K]V1, M2 ~map[K]V2, K comparable, V1, V2 any](m1 M1, m2 M2, eq func(V1, V2) bool) bool
Examples ¶
Functions ¶
func Clone ¶
func Clone[M ~map[K]V, K comparable, V any](m M) M
Clone returns a copy of m. This is a shallow clone: the new keys and values are set using ordinary assignment.
func Copy ¶
func Copy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2)
Copy copies all key/value pairs in src adding them to dst. When a key in src is already present in dst, the value in dst will be overwritten by the value associated with the key in src.
func DeleteFunc ¶
func DeleteFunc[M ~map[K]V, K comparable, V any](m M, del func(K, V) bool)
DeleteFunc deletes any key/value pairs from m for which del returns true.
func Equal ¶
func Equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool
Equal reports whether two maps contain the same key/value pairs. Values are compared using ==.
func EqualFunc ¶
func EqualFunc[M1 ~map[K]V1, M2 ~map[K]V2, K comparable, V1, V2 any](m1 M1, m2 M2, eq func(V1, V2) bool) bool
EqualFunc is like Equal, but compares values using eq.
Keys are still compared with ==.
Code:play
Output:Example¶
package main
import (
"fmt"
"maps"
"strings"
)
func main() {
m1 := map[int]string{
1: "one",
10: "Ten",
1000: "THOUSAND",
}
m2 := map[int][]byte{
1: []byte("One"),
10: []byte("Ten"),
1000: []byte("Thousand"),
}
eq := maps.EqualFunc(m1, m2, func(v1 string, v2 []byte) bool {
return strings.ToLower(v1) == strings.ToLower(string(v2))
})
fmt.Println(eq)
}
true
Source Files ¶
- Version
- v1.21.11
- Published
- May 30, 2024
- Platform
- windows/amd64
- Last checked
- 15 seconds ago –
Tools for package owners.