Documentation
¶
Overview ¶
Package maps provides utility functions for handling maps, and transitioning between maps and slices.
Deprecated: use https://pkg.go.dev/golang.org/x/exp/maps
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Dedup ¶
func Dedup(a interface{}) interface{}
Dedup returns a copy of the given slice, sorted and with no duplicated values. For non-comparable types, removes duplicates without sorting. Input slice is unchanged.
func Keys ¶
func Keys(a interface{}) interface{}
Keys returns the keys of the given map in a sorted slice.
For a map of type map[k]v, result is of type []k.
Example ¶
m := map[string]int{
"a": 1,
"c": 3,
"b": 2,
}
keys := Keys(m).([]string)
fmt.Println(keys)
Output: [a b c]
func Map ¶ added in v0.1.7
func Map(a interface{}, f interface{}) interface{}
Map creates a map using the elements in slice a and the values obtaned by calling f on those elements. If a is of type []K then f needs to be of type func(K) V and the result will be of type map[K]V. Panics if the types are not as expected.
func Of ¶
func Of(slice interface{}, value interface{}) interface{}
Of returns a map whose keys are the values of the given slice, and all have the same given value.
For a slice of type []k and value of type v, result is of type map[k]v.
Example ¶
people := []string{"alice", "bob"}
m := Of(people, true).(map[string]bool)
fmt.Println("'alice' in map:", m["alice"])
fmt.Println("'bob' in map:", m["bob"])
fmt.Println("'charles' in map:", m["charles"])
Output: 'alice' in map: true 'bob' in map: true 'charles' in map: false
Types ¶
This section is empty.