dict

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package dict contains map-related functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultGet

func DefaultGet[K comparable, V any](items map[K]V, key K, defaultValue V) V

Get the value of key, or return default value if key is not in map

func Filter

func Filter[K comparable, V any](items map[K]V, keep func(K, V) bool) map[K]V

Filter map: only keep entries that pass keep function

func Flags

func Flags[T comparable](items []T, flag bool) map[T]bool

Create new boolean map, with each item initialized to flag boolean

func FromStruct

func FromStruct[T, V any](structRef *T) (map[string]V, error)

Create map from given struct pointer

func Get

func Get[T any](obj Object, key string) (T, bool)

Get value = obj[key], then type coerce into T

func GetListRef

func GetListRef[T any](obj Object, key string) []*T

Get value = obj[key] then type coerce into []*T

func GetRef

func GetRef[T any](obj Object, key string) *T

Get value = obj[key], then type coerce into *T

func GroupByFunc

func GroupByFunc[K, B comparable, V, A any](items map[K]V, keyFn func(K) A, valueFn func(V) B) map[B][]A

Group data by values, using key and value transformers

func GroupByFuncList

func GroupByFuncList[K, B comparable, V, A any](items map[K][]V, keyFn func(K) A, valueFn func(V) B) map[B][]A

Group data (map[K][]V) by values => map[V][]K, using key and value transformers

func GroupByValue

func GroupByValue[K, V comparable](items map[K]V) map[V][]K

Group data by values

func GroupByValueList

func GroupByValueList[K, V comparable](items map[K][]V) map[V][]K

Group data (map[K][]V) by values => map[V][]K

func HasKey

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

Check if map has key

func HasValue

func HasValue[K, V comparable](items map[K]V, value V) bool

Check if map has value

func Inspect

func Inspect[T any](structRef *T) string

Inspect displays a struct as key-value pairs per line

func Keys

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

Get map keys

func Length

func Length[K comparable, V any](items map[K]V) int

Get map length

func LookupKey

func LookupKey[K comparable, V any](items map[K]V) func(K) V

Creates a converter function that maps the key to the associated value, Panics if key not found

func MergeCounts

func MergeCounts[K comparable](counters []map[K]int) map[K]int

Merge the counts from counter maps into one map

func NoKey

func NoKey[K comparable, V any](items map[K]V, key K) bool

Check if map has no key

func NoValue

func NoValue[K, V comparable](items map[K]V, value V) bool

Check if map has no value

func SetDefault

func SetDefault[K comparable, V any](items map[K]V, key K, defaultValue V)

Set default value if key is not in map

func SortValues

func SortValues[K comparable, V cmp.Ordered](items map[K][]V)

Sort the list of values for each key

func Swap

func Swap[K, V comparable](items map[K]V) map[V]K

Swap keys and values, convert map[K]V to map[V]K, Can lose data if values are not unique

func SwapList

func SwapList[K, V comparable](items map[K][]V) map[V]K

Convert map[K][]V to map[V]K, Can lose data if values are not unique

func TallyValues

func TallyValues[K, V comparable](items map[K]V, values []V) map[V]int

Create tally of how many times each value appears in the map

func ToStruct

func ToStruct[T any](obj Object) (*T, error)

Create struct from given Object

func Unzip

func Unzip[K comparable, V any](items map[K]V) ([]K, []V)

Unzip the map, return list of keys and values, Order of keys is same as order of corresponding values

func Update

func Update[K comparable, V any](oldMap, newMap map[K]V) map[K]V

Add entries of new map into old map, returns old map. If there are key conflicts, new map entries overwrite the old map entries.

func UpdateCounter

func UpdateCounter[T comparable](counter Counter[T], items []T)

Update the counter with the incoming items

func UpdateCounts

func UpdateCounts[K comparable](oldCounter, newCounter map[K]int)

Update the old counter with counts from new counter

func Values

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

Get map values

func Zip

func Zip[K comparable, V any](keys []K, values []V) map[K]V

Zip list of keys and values to create map

Types

type BoolMap

type BoolMap = map[string]bool

type Counter

type Counter[T comparable] = map[T]int

func CounterFunc

func CounterFunc[T any, K comparable](items []T, key func(T) K) Counter[K]

Create counter, with keys produced from keyFn

func NewCounter

func NewCounter[T comparable](items []T) Counter[T]

Create new counter, with each item initialized to count=0

type Entry

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

Key-Value pair

func Entries

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

Get map entries

func SortedEntries

func SortedEntries[K cmp.Ordered, V any](items map[K]V) []Entry[K, V]

Get map entries, sorted by keys

func (Entry[K, V]) Tuple

func (e Entry[K, V]) Tuple() (K, V)

Return Entry as Key, Value

type IntCounter

type IntCounter = map[int]int

type IntMap

type IntMap = map[string]int

type Lookup

type Lookup[K comparable, V any] = map[K]V

type Object

type Object = map[string]any

func Prune

func Prune[T any](structRef *T, fieldNames ...string) (Object, error)

Create Object from struct, but only keep given fieldNames

func ToObject

func ToObject[T any](structRef *T) (Object, error)

Create Object from given struct pointer

type StringCounter

type StringCounter = map[string]int

type StringListMap

type StringListMap = map[string][]string

type StringMap

type StringMap = map[string]string

type SyncMap

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

Concurrency-safe generic map

func NewSyncMap

func NewSyncMap[K comparable, V any]() *SyncMap[K, V]

Create new SyncMap

func SyncMapFrom

func SyncMapFrom[K comparable, V any](items map[K]V) *SyncMap[K, V]

Create SyncMap from existing map

func (*SyncMap[K, V]) Clear

func (sm *SyncMap[K, V]) Clear()

Clear SyncMap's underlying map data

func (*SyncMap[K, V]) ClearMap

func (sm *SyncMap[K, V]) ClearMap() map[K]V

Copy SyncMap's underlying map and clear it

func (*SyncMap[K, V]) Delete

func (sm *SyncMap[K, V]) Delete(key K)

SyncMap.Delete

func (*SyncMap[K, V]) DeleteKeys

func (sm *SyncMap[K, V]) DeleteKeys(keys []K)

SyncMap.DeleteKeys

func (*SyncMap[K, V]) Get

func (sm *SyncMap[K, V]) Get(key K) (V, bool)

SyncMap.Get

func (*SyncMap[K, V]) Keys

func (sm *SyncMap[K, V]) Keys() []K

SyncMap's underlying map keys

func (*SyncMap[K, V]) Len

func (sm *SyncMap[K, V]) Len() int

SyncMap number of items

func (*SyncMap[K, V]) Map

func (sm *SyncMap[K, V]) Map() map[K]V

SyncMap's underlying map

func (*SyncMap[K, V]) Set

func (sm *SyncMap[K, V]) Set(key K, value V)

SyncMap.Set

func (*SyncMap[K, V]) SetIf

func (sm *SyncMap[K, V]) SetIf(key K, value V, isValid func(V) bool) bool

SyncMap.SetIf

func (*SyncMap[K, V]) Values

func (sm *SyncMap[K, V]) Values() []V

SyncMap's underlying map values

type UintMap

type UintMap = map[string]uint

Jump to

Keyboard shortcuts

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