Documentation ¶
Overview ¶
Package m provides functions for working with maps.
Example
import m "github.com/neurocode-io/go-pkgs/map" input := map[string]int{"a": 1, "b": 2, "c": 3} keys := m.Keys(input) // keys == []string{"a", "b", "c"}
Index ¶
- func Entries[T comparable](input map[T]T) [][2]T
- func FromEntries[T comparable](entries [][2]T) map[T]T
- func Keys[T comparable, V any](input map[T]V) []T
- func Sort[T constraints.Ordered](input map[T]T) map[T]T
- func SortByValue[T constraints.Ordered](input map[T]T) map[T]T
- func Sum[T comparable, V constraints.Float | constraints.Integer](input map[T]V) V
- func Values[T comparable, V any](input map[T]V) []V
- type AsyncMap
- func (m *AsyncMap[T, V]) Delete(key T)
- func (m *AsyncMap[T, V]) Load(key T) (value V, ok bool)
- func (m *AsyncMap[T, V]) LoadAndDelete(key T) (value V, ok bool)
- func (m *AsyncMap[T, V]) LoadOrStore(key T, value V) (V, bool)
- func (m *AsyncMap[T, V]) Range(fn func(key T, value V) bool)
- func (m *AsyncMap[T, V]) Store(key T, value V)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Entries ¶
func Entries[T comparable](input map[T]T) [][2]T
Entries returns a slice of the entries in the map.
Example
input := map[string]int{"a": 1, "b": 2, "c": 3} entries := m.Entries(input) // entries == [][2]string{{"a", 1}, {"b", 2}, {"c", 3}}
func FromEntries ¶
func FromEntries[T comparable](entries [][2]T) map[T]T
FromEntries returns a map from the entries in the slice.
Example
input := [][2]string{{"a", 1}, {"b", 2}, {"c", 3}} m := m.FromEntries(input) // m == map[string]int{"a": 1, "b": 2, "c": 3}
func Keys ¶
func Keys[T comparable, V any](input map[T]V) []T
Keys returns a slice of the keys in the map.
Example
input := map[string]int{"a": 1, "b": 2, "c": 3} keys := m.Keys(input) // keys == []string{"a", "b", "c"}
func Sort ¶
func Sort[T constraints.Ordered](input map[T]T) map[T]T
Sort returns a map sorted by the keys.
Example
input := map[string]string{"b": "world", "a": "hello", "c": "!"} m := m.Sort(input) // m == map[string]string{"a": "hello", "b": "world", "c": "!"}
func SortByValue ¶
func SortByValue[T constraints.Ordered](input map[T]T) map[T]T
SortByValue returns a map sorted by the values.
Example
input := map[string]int{"a": 9, "b": 1, "c": 3} m := m.SortByValue(input) // m == map[string]int{"b": 1, "c": 3, "a": 9}
func Sum ¶
func Sum[T comparable, V constraints.Float | constraints.Integer](input map[T]V) V
Sum returns the sum of the values in the map.
Example
input := map[string]int{"a": 1, "b": 2, "c": 3} sum := m.Sum(input) // sum == 6
func Values ¶
func Values[T comparable, V any](input map[T]V) []V
Values returns a slice of the values in the map.
Example
input := map[string]int{"a": 1, "b": 2, "c": 3} values := m.Values(input) // values == []int{1, 2, 3}
Types ¶
type AsyncMap ¶
type AsyncMap[T comparable, V any] struct { // contains filtered or unexported fields }
AsyncMap is a generic threadsafe map. It can be used as a drop-in replacement for sync.Map
Example
AsyncMap := NewAsyncMap[string, int]() AsyncMap.Store("a", 1) AsyncMap.Store("b", 2) AsyncMap.Load("a") // 1, true
func NewAsyncMap ¶
func NewAsyncMap[T comparable, V any]() *AsyncMap[T, V]
NewAsyncMap returns a new pointer to an asyncMap.
func (*AsyncMap[T, V]) Delete ¶
func (m *AsyncMap[T, V]) Delete(key T)
Delete deletes the value for a key. If the key does not exist, it does nothing.
Example
asyncMap := NewAsyncMap[string, int]() asyncMap.Store("a", 1) asyncMap.Delete("a")
func (*AsyncMap[T, V]) Load ¶
Load returns the value and true if the key exists in the map, otherwise it returns the zero value and false.
Example
asyncMap := NewAsyncMap[string, int]() asyncMap.Store("a", 1) asyncMap.Load("b") // 0, false asyncMap.Load("a") // 1, true
func (*AsyncMap[T, V]) LoadAndDelete ¶
LoadAndDelete deletes the value for a key, returning the previous value if any.
Example
asyncMap := NewAsyncMap[string, int]() asyncMap.Store("a", 1) asyncMap.LoadAndDelete("a") // 1, true asyncMap.LoadAndDelete("a") // 0, false
func (*AsyncMap[T, V]) LoadOrStore ¶
LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.
Example
asyncMap := NewAsyncMap[string, int]() asyncMap.LoadOrStore("a", 1) // 1, false asyncMap.LoadOrStore("a", 2) // 1, true
func (*AsyncMap[T, V]) Range ¶
Range calls fn sequentially for each key and value present in the map. If fn returns false, range stops the iteration.
Example
asyncMap := NewAsyncMap[string, int]() asyncMap.Store("a", 1) asyncMap.Store("b", 2) fn := func(key string, value int) bool { fmt.Printf("%s: %d", key, value) return true } asyncMap.Range(fn)