Documentation
¶
Overview ¶
Package maps provides some functions for working with Go maps.
Index ¶
- func Clear[K comparable, V any](m map[K]V)
- func Clone[K comparable, V any](m map[K]V) map[K]V
- func Contains[K comparable, V any](m map[K]V, key K) bool
- func Delete[K comparable, V any](m map[K]V, fn func(k K, v V) bool) int
- func Equal[K, V comparable](m1, m2 map[K]V) bool
- func EqualFunc[K comparable, V any](m1, m2 map[K]V, equal func(v1, v2 V) bool) bool
- func FromFuncs[K comparable, V any](size int, keys func() K, values func() V) map[K]V
- func FromItems[K comparable, V any](items []Item[K, V]) map[K]V
- func FromSlices[K comparable, V any](keys []K, values []V) map[K]V
- func Get[K comparable, V any](m map[K]V, key K, dflt V) V
- func Keys[K comparable, V any](m map[K]V) []K
- func KeysForValue[K, V comparable](m map[K]V, value V) []K
- func KeysForValueFunc[K comparable, V any](m map[K]V, value V, equal func(v1, v2 V) bool) []K
- func Merge[K comparable, V any](m1, m2 map[K]V) map[K]V
- func Update[K comparable, V any](m1, m2 map[K]V)
- func Values[K comparable, V any](m map[K]V) []V
- type Item
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clone ¶
func Clone[K comparable, V any](m map[K]V) map[K]V
Clone returns a shallow copy of map m.
func Contains ¶
func Contains[K comparable, V any](m map[K]V, key K) bool
Contains returns true if key is in map m.
func Delete ¶ added in v0.3.0
func Delete[K comparable, V any](m map[K]V, fn func(k K, v V) bool) int
Delete deletes all items from map m for which fn returns true and returns the number of deleted items.
func Equal ¶
func Equal[K, V comparable](m1, m2 map[K]V) bool
Equal returns true if the two maps are equal (containing the same keys with the same values).
func EqualFunc ¶
func EqualFunc[K comparable, V any](m1, m2 map[K]V, equal func(v1, v2 V) bool) bool
EqualFunc returns true if the two maps are equal using a function to compare values.
func FromFuncs ¶ added in v0.2.0
func FromFuncs[K comparable, V any](size int, keys func() K, values func() V) map[K]V
FromFuncs makes a map from the return values of two functions (e.g. from math.random). Panics if the keys functions is nil or size is negative. If the values function is nil, the zero value of type V will be used for all values.
func FromItems ¶
func FromItems[K comparable, V any](items []Item[K, V]) map[K]V
FromItems makes a map from a slice of Item objects.
func FromSlices ¶
func FromSlices[K comparable, V any](keys []K, values []V) map[K]V
FromSlices makes a map from two slices. If the keys slice is longer then the values slice, the surplus keys will have the zero value of type V as their value. Surplus values will be ignored.
func Get ¶
func Get[K comparable, V any](m map[K]V, key K, dflt V) V
Get returns the value for key from map m or a default value.
func Keys ¶
func Keys[K comparable, V any](m map[K]V) []K
Keys returns a slice with all keys from map m.
func KeysForValue ¶
func KeysForValue[K, V comparable](m map[K]V, value V) []K
KeysForValue returns a slice with keys which have the given value.
func KeysForValueFunc ¶
func KeysForValueFunc[K comparable, V any](m map[K]V, value V, equal func(v1, v2 V) bool) []K
KeysForValueFunc returns a slice with keys which have the given value using a function to compare values.
func Merge ¶ added in v0.4.0
func Merge[K comparable, V any](m1, m2 map[K]V) map[K]V
Merge merges two maps and returns a new map. If a key is present in both maps, the value in m1 will be overwritten with the value from m2. Panics if m1 is nil.
func Update ¶
func Update[K comparable, V any](m1, m2 map[K]V)
Update updates map m1 with items from map m2. If a key is present in both maps, the value in m1 will be overwritten with the value from m2. Panics if m1 is nil.
func Values ¶
func Values[K comparable, V any](m map[K]V) []V
Values returns a slice with all values from map m.
Types ¶
type Item ¶
type Item[K comparable, V any] struct { Key K Value V }
Type Item represents a key-value pair.