maputil

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V

Filter iterates over map, return a new map contains all key and value pairs pass the predicate function.

func FilterByKeys

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

FilterByKeys iterates over map, return a new map whose keys are all given keys.

func FilterByValues

func FilterByValues[K comparable, V comparable](m map[K]V, values []V) map[K]V

FilterByValues iterates over map, return a new map whose values are all given values.

func ForEach

func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V))

ForEach executes iteratee funcation for every key and value pair in map.

func FromEntries

func FromEntries[K comparable, V any](entries []Entry[K, V]) map[K]V

FromEntries creates a map based on a slice of key/value pairs

func HasKey

func HasKey[K comparable, V any](m map[K]V, key K) bool

HasKey checks if map has key or not. This function is used to replace the following boilerplate code: _, haskey := amap["baz"];

if haskey {
   fmt.Println("map has key baz")
}

func Intersect

func Intersect[K comparable, V any](maps ...map[K]V) map[K]V

Intersect iterates over maps, return a new map of key and value pairs in all given maps.

func IsDisjoint

func IsDisjoint[K comparable, V any](mapA, mapB map[K]V) bool

IsDisjoint two map are disjoint if they have no keys in common.

func Keys

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

Keys returns a slice of the map's keys.

func KeysBy

func KeysBy[K comparable, V any, T any](m map[K]V, mapper func(item K) T) []T

KeysBy creates a slice whose element is the result of function mapper invoked by every map's key.

func MapKeys

func MapKeys[K comparable, V any, T comparable](m map[K]V, iteratee func(key K, value V) T) map[T]V

MapKeys transforms a map to other type map by manipulating it's keys.

func MapTo

func MapTo(src any, dst any) error

MapTo try to map any interface to struct or base type

Eg:
	v := map[string]interface{}{
		"service":map[string]interface{}{
			"ip":"127.0.0.1",
			"port":1234,
		},
		version:"v1.0.01"
	}

	type Target struct {
		Service struct {
			Ip string `json:"ip"`
			Port int `json:"port"`
		} `json:"service"`
		Ver string `json:"version"`
	}

	var dist Target
	if err := maputil.MapTo(v,&dist); err != nil {
		log.Println(err)
		return
	}

	log.Println(dist)

func MapValues

func MapValues[K comparable, V any, T any](m map[K]V, iteratee func(key K, value V) T) map[K]T

MapValues transforms a map to other type map by manipulating it's values.

func Merge

func Merge[K comparable, V any](maps ...map[K]V) map[K]V

Merge maps, next key will overwrite previous key.

func Minus

func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V

Minus creates a map of whose key in mapA but not in mapB.

func OmitBy

func OmitBy[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V

OmitBy is the opposite of Filter, removes all the map elements for which the predicate function returns true.

func OmitByKeys

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

OmitByKeys the opposite of FilterByKeys, extracts all the map elements which keys are not omitted.

func OmitByValues

func OmitByValues[K comparable, V comparable](m map[K]V, values []V) map[K]V

OmitByValues the opposite of FilterByValues. remov all elements whose value are in the give slice.

func Transform

func Transform[K1 comparable, V1 any, K2 comparable, V2 any](m map[K1]V1, iteratee func(key K1, value V1) (K2, V2)) map[K2]V2

Transform a map to another type map.

func Values

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

Values returns a slice of the map's values.

func ValuesBy

func ValuesBy[K comparable, V any, T any](m map[K]V, mapper func(item V) T) []T

ValuesBy creates a slice whose element is the result of function mapper invoked by every map's value.

Types

type ConcurrentMap

type ConcurrentMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

ConcurrentMap is like map, but is safe for concurrent use by multiple goroutines.

func NewConcurrentMap

func NewConcurrentMap[K comparable, V any](shardCount int) *ConcurrentMap[K, V]

NewConcurrentMap create a ConcurrentMap with specific shard count.

func (*ConcurrentMap[K, V]) Delete

func (cm *ConcurrentMap[K, V]) Delete(key K)

Delete the value for a key.

func (*ConcurrentMap[K, V]) Get

func (cm *ConcurrentMap[K, V]) Get(key K) (V, bool)

Get the value stored in the map for a key, or nil if no.

func (*ConcurrentMap[K, V]) GetAndDelete

func (cm *ConcurrentMap[K, V]) GetAndDelete(key K) (actual V, ok bool)

GetAndDelete returns the existing value for the key if present and then delete the value for the key. Otherwise, do nothing, just return false

func (*ConcurrentMap[K, V]) GetOrSet

func (cm *ConcurrentMap[K, V]) GetOrSet(key K, value V) (actual V, ok bool)

GetOrSet returns the existing value for the key if present. Otherwise, it sets and returns the given value.

func (*ConcurrentMap[K, V]) Has

func (cm *ConcurrentMap[K, V]) Has(key K) bool

Has checks if map has the value for a key.

func (*ConcurrentMap[K, V]) Range

func (cm *ConcurrentMap[K, V]) Range(iterator func(key K, value V) bool)

Range calls iterator sequentially for each key and value present in each of the shards in the map. If iterator returns false, range stops the iteration.

func (*ConcurrentMap[K, V]) Set

func (cm *ConcurrentMap[K, V]) Set(key K, value V)

Set the value for a key.

type Entry

type Entry[K comparable, V any] struct {
	Key   K
	Value V
}

Entry is a key/value pairs.

func Entries

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

Entries transforms a map into array of key/value pairs.

Jump to

Keyboard shortcuts

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