maps

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2023 License: BSD-3-Clause Imports: 0 Imported by: 1

README

maps

Functions for working with Go maps.

Documentation: https://pkg.go.dev/github.com/andreas19/maps

Documentation

Overview

Package maps provides some functions for working with Go maps.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clear

func Clear[K comparable, V any](m map[K]V)

Clear removes all items from map m.

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.

func Items

func Items[K comparable, V any](m map[K]V) []Item[K, V]

Items returns a slice of Item objects for map m.

Jump to

Keyboard shortcuts

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