maps

package
v0.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package maps provides utility functions for handling maps, and transitioning between maps and slices.

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 deprecated

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.

Deprecated: use Map instead.

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

func Values

func Values(a interface{}) interface{}

Values returns the values of the given map in a sorted slice. Duplicates are kept.

For a map of type map[k]v, result is of type []v.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL