Documentation
¶
Index ¶
- func EqualFunc[K comparable, V1, V2 any](m1 MapI[K, V1], m2 MapI[K, V2], eq func(V1, V2) bool) bool
- type Equaler
- type Getter
- type Loader
- type Map
- func (m *Map[K, V]) All() iter.Seq2[K, V]
- func (m *Map[K, V]) Clear()
- func (m *Map[K, V]) Clone() *Map[K, V]
- func (m *Map[K, V]) Copy(in MapI[K, V])
- func (m Map[K, V]) Delete(k K) V
- func (m *Map[K, V]) DeleteFunc(del func(K, V) bool)
- func (m *Map[K, V]) Equal(m2 MapI[K, V]) bool
- func (m *Map[K, V]) Get(k K) V
- func (m *Map[K, V]) Has(k K) bool
- func (m *Map[K, V]) Insert(seq iter.Seq2[K, V])
- func (m *Map[K, V]) Keys() []K
- func (m *Map[K, V]) KeysIter() iter.Seq[K]
- func (m *Map[K, V]) Len() int
- func (m *Map[K, V]) Load(k K) (V, bool)
- func (m *Map[K, V]) MarshalBinary() ([]byte, error)
- func (m *Map[K, V]) MarshalJSON() (out []byte, err error)
- func (m *Map[K, V]) Merge(in MapI[K, V])
- func (m *Map[K, V]) Range(f func(k K, v V) bool)
- func (m *Map[K, V]) Set(k K, v V)
- func (m *Map[K, V]) String() string
- func (m *Map[K, V]) UnmarshalBinary(data []byte) (err error)
- func (m *Map[K, V]) UnmarshalJSON(in []byte) (err error)
- func (m *Map[K, V]) Values() []V
- func (m *Map[K, V]) ValuesIter() iter.Seq[V]
- type MapI
- type OrderedSet
- func (m *OrderedSet[K]) Add(k ...K) SetI[K]
- func (m *OrderedSet[K]) All() iter.Seq[K]
- func (m *OrderedSet[K]) Clear()
- func (m *OrderedSet[K]) Clone() *OrderedSet[K]
- func (m *OrderedSet[K]) Copy(in SetI[K])
- func (m *OrderedSet[K]) Delete(k K)
- func (m *OrderedSet[K]) DeleteFunc(del func(K) bool)
- func (m *OrderedSet[K]) Equal(m2 SetI[K]) bool
- func (m *OrderedSet[K]) Has(k K) bool
- func (m *OrderedSet[K]) Insert(seq iter.Seq[K])
- func (m *OrderedSet[K]) Len() int
- func (m *OrderedSet[K]) MarshalJSON() (out []byte, err error)
- func (m *OrderedSet[K]) Range(f func(k K) bool)
- func (m *OrderedSet[K]) String() string
- func (m *OrderedSet[K]) Values() []K
- type SafeMap
- func (m *SafeMap[K, V]) All() iter.Seq2[K, V]
- func (m *SafeMap[K, V]) Clear()
- func (m *SafeMap[K, V]) Clone() *SafeMap[K, V]
- func (m *SafeMap[K, V]) Copy(in MapI[K, V])
- func (m *SafeMap[K, V]) Delete(k K) (v V)
- func (m *SafeMap[K, V]) DeleteFunc(del func(K, V) bool)
- func (m *SafeMap[K, V]) Equal(m2 MapI[K, V]) bool
- func (m *SafeMap[K, V]) Get(k K) (v V)
- func (m *SafeMap[K, V]) Has(k K) (exists bool)
- func (m *SafeMap[K, V]) Insert(seq iter.Seq2[K, V])
- func (m *SafeMap[K, V]) Keys() (keys []K)
- func (m *SafeMap[K, V]) KeysIter() iter.Seq[K]
- func (m *SafeMap[K, V]) Len() (l int)
- func (m *SafeMap[K, V]) Load(k K) (v V, ok bool)
- func (m *SafeMap[K, V]) MarshalBinary() ([]byte, error)
- func (m *SafeMap[K, V]) MarshalJSON() (out []byte, err error)
- func (m *SafeMap[K, V]) Merge(in MapI[K, V])
- func (m *SafeMap[K, V]) Range(f func(k K, v V) bool)
- func (m *SafeMap[K, V]) Set(k K, v V)
- func (m *SafeMap[K, V]) String() string
- func (m *SafeMap[K, V]) UnmarshalBinary(data []byte) (err error)
- func (m *SafeMap[K, V]) UnmarshalJSON(in []byte) (err error)
- func (m *SafeMap[K, V]) Values() (v []V)
- func (m *SafeMap[K, V]) ValuesIter() iter.Seq[V]
- type SafeSliceMap
- func (m *SafeSliceMap[K, V]) All() iter.Seq2[K, V]
- func (m *SafeSliceMap[K, V]) Clear()
- func (m *SafeSliceMap[K, V]) Clone() *SafeSliceMap[K, V]
- func (m *SafeSliceMap[K, V]) Copy(in MapI[K, V])
- func (m *SafeSliceMap[K, V]) Delete(key K) (val V)
- func (m *SafeSliceMap[K, V]) DeleteFunc(del func(K, V) bool)
- func (m *SafeSliceMap[K, V]) Equal(m2 MapI[K, V]) bool
- func (m *SafeSliceMap[K, V]) Get(key K) (val V)
- func (m *SafeSliceMap[K, V]) GetAt(position int) (val V)
- func (m *SafeSliceMap[K, V]) GetKeyAt(position int) (key K)
- func (m *SafeSliceMap[K, V]) Has(key K) (ok bool)
- func (m *SafeSliceMap[K, V]) Insert(seq iter.Seq2[K, V])
- func (m *SafeSliceMap[K, V]) Keys() (keys []K)
- func (m *SafeSliceMap[K, V]) KeysIter() iter.Seq[K]
- func (m *SafeSliceMap[K, V]) Len() int
- func (m *SafeSliceMap[K, V]) Load(key K) (val V, ok bool)
- func (m *SafeSliceMap[K, V]) MarshalBinary() (data []byte, err error)
- func (m *SafeSliceMap[K, V]) MarshalJSON() (data []byte, err error)
- func (m *SafeSliceMap[K, V]) Merge(in MapI[K, V])
- func (m *SafeSliceMap[K, V]) Range(f func(key K, value V) bool)
- func (m *SafeSliceMap[K, V]) Set(key K, val V)
- func (m *SafeSliceMap[K, V]) SetAt(index int, key K, val V)
- func (m *SafeSliceMap[K, V]) SetSortFunc(f func(key1, key2 K, val1, val2 V) bool)
- func (m *SafeSliceMap[K, V]) String() string
- func (m *SafeSliceMap[K, V]) UnmarshalBinary(data []byte) (err error)
- func (m *SafeSliceMap[K, V]) UnmarshalJSON(data []byte) (err error)
- func (m *SafeSliceMap[K, V]) Values() (values []V)
- func (m *SafeSliceMap[K, V]) ValuesIter() iter.Seq[V]
- type Set
- func (m *Set[K]) Add(k ...K) SetI[K]
- func (m *Set[K]) All() iter.Seq[K]
- func (m *Set[K]) Clear()
- func (m *Set[K]) Clone() *Set[K]
- func (m *Set[K]) Copy(in SetI[K])
- func (m *Set[K]) Delete(k K)
- func (m *Set[K]) DeleteFunc(del func(K) bool)
- func (m *Set[K]) Equal(m2 SetI[K]) bool
- func (m *Set[K]) Has(k K) bool
- func (m *Set[K]) Insert(seq iter.Seq[K])
- func (m *Set[K]) Len() int
- func (m *Set[K]) MarshalBinary() ([]byte, error)
- func (m *Set[K]) MarshalJSON() (out []byte, err error)
- func (m *Set[K]) Merge(in SetI[K])
- func (m *Set[K]) Range(f func(k K) bool)
- func (m *Set[K]) String() string
- func (m *Set[K]) UnmarshalBinary(data []byte) (err error)
- func (m *Set[K]) UnmarshalJSON(in []byte) (err error)
- func (m *Set[K]) Values() []K
- type SetI
- type Setter
- type SliceMap
- func (m *SliceMap[K, V]) All() iter.Seq2[K, V]
- func (m *SliceMap[K, V]) Clear()
- func (m *SliceMap[K, V]) Clone() *SliceMap[K, V]
- func (m *SliceMap[K, V]) Copy(in MapI[K, V])
- func (m *SliceMap[K, V]) Delete(key K) (val V)
- func (m *SliceMap[K, V]) DeleteFunc(del func(K, V) bool)
- func (m *SliceMap[K, V]) Equal(m2 MapI[K, V]) bool
- func (m *SliceMap[K, V]) Get(key K) (val V)
- func (m *SliceMap[K, V]) GetAt(position int) (val V)
- func (m *SliceMap[K, V]) GetKeyAt(position int) (key K)
- func (m *SliceMap[K, V]) Has(key K) (ok bool)
- func (m *SliceMap[K, V]) Insert(seq iter.Seq2[K, V])
- func (m *SliceMap[K, V]) Keys() (keys []K)
- func (m *SliceMap[K, V]) KeysIter() iter.Seq[K]
- func (m *SliceMap[K, V]) Len() int
- func (m *SliceMap[K, V]) Load(key K) (val V, ok bool)
- func (m *SliceMap[K, V]) MarshalBinary() (data []byte, err error)
- func (m *SliceMap[K, V]) MarshalJSON() (data []byte, err error)
- func (m *SliceMap[K, V]) Merge(in MapI[K, V])
- func (m *SliceMap[K, V]) Range(f func(key K, value V) bool)
- func (m *SliceMap[K, V]) Set(key K, val V)
- func (m *SliceMap[K, V]) SetAt(index int, key K, val V)
- func (m *SliceMap[K, V]) SetSortFunc(f func(key1, key2 K, val1, val2 V) bool)
- func (m *SliceMap[K, V]) String() string
- func (m *SliceMap[K, V]) UnmarshalBinary(data []byte) (err error)
- func (m *SliceMap[K, V]) UnmarshalJSON(data []byte) (err error)
- func (m *SliceMap[K, V]) Values() (values []V)
- func (m *SliceMap[K, V]) ValuesIter() iter.Seq[V]
- type SliceSet
- func (m *SliceSet[K]) Add(k ...K) SetI[K]
- func (m *SliceSet[K]) All() iter.Seq[K]
- func (m *SliceSet[K]) Clear()
- func (m *SliceSet[K]) Clone() *SliceSet[K]
- func (m *SliceSet[K]) Copy(in SetI[K])
- func (m *SliceSet[K]) Delete(k K)
- func (m *SliceSet[K]) DeleteFunc(del func(K) bool)
- func (m *SliceSet[K]) Equal(m2 SetI[K]) bool
- func (m *SliceSet[K]) Has(k K) bool
- func (m *SliceSet[K]) Insert(seq iter.Seq[K])
- func (m *SliceSet[K]) Len() int
- func (m *SliceSet[K]) MarshalBinary() ([]byte, error)
- func (m *SliceSet[K]) MarshalJSON() (out []byte, err error)
- func (m *SliceSet[K]) Merge(in SetI[K])
- func (m *SliceSet[K]) Range(f func(k K) bool)
- func (m *SliceSet[K]) SetSortFunc(f func(val1, val2 K) bool)
- func (m *SliceSet[K]) String() string
- func (m *SliceSet[K]) UnmarshalBinary(data []byte) (err error)
- func (m *SliceSet[K]) UnmarshalJSON(in []byte) (err error)
- func (m *SliceSet[K]) Values() []K
- type StdMap
- func (m StdMap[K, V]) All() iter.Seq2[K, V]
- func (m StdMap[K, V]) Clear()
- func (m StdMap[K, V]) Clone() StdMap[K, V]
- func (m StdMap[K, V]) Copy(in MapI[K, V])
- func (m StdMap[K, V]) Delete(k K) (v V)
- func (m StdMap[K, V]) DeleteFunc(del func(K, V) bool)
- func (m StdMap[K, V]) Equal(m2 MapI[K, V]) bool
- func (m StdMap[K, V]) Get(k K) (v V)
- func (m StdMap[K, V]) Has(k K) (exists bool)
- func (m StdMap[K, V]) Insert(seq iter.Seq2[K, V])
- func (m StdMap[K, V]) Keys() (keys []K)
- func (m StdMap[K, V]) KeysIter() iter.Seq[K]
- func (m StdMap[K, V]) Len() int
- func (m StdMap[K, V]) Load(k K) (v V, ok bool)
- func (m StdMap[K, V]) MarshalBinary() ([]byte, error)
- func (m StdMap[K, V]) MarshalJSON() (out []byte, err error)
- func (m StdMap[K, V]) Merge(in MapI[K, V])
- func (m StdMap[K, V]) Range(f func(k K, v V) bool)
- func (m StdMap[K, V]) Set(k K, v V)
- func (m StdMap[K, V]) String() string
- func (m *StdMap[K, V]) UnmarshalBinary(data []byte) (err error)
- func (m *StdMap[K, V]) UnmarshalJSON(in []byte) (err error)
- func (m StdMap[K, V]) Values() (values []V)
- func (m StdMap[K, V]) ValuesIter() iter.Seq[V]
Examples ¶
- Cast
- CollectMap
- Map.String
- OrderedSet.String
- SafeMap.String
- SafeSliceMap.GetAt
- SafeSliceMap.GetKeyAt
- SafeSliceMap.SetAt
- SafeSliceMap.SetSortFunc
- Set.String
- SliceMap.GetAt
- SliceMap.GetKeyAt
- SliceMap.SetAt
- SliceMap.SetSortFunc
- SliceSet.String
- StdMap.All
- StdMap.DeleteFunc
- StdMap.KeysIter
- StdMap.ValuesIter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Equaler ¶
Equaler is the interface that implements an Equal function and that provides a way for the various MapI like objects to determine if they are equal.
In particular, if your Map has non-comparable values, like a slice, but you would still like to call Equal() on that map, define an Equal function on the values to do the comparison. For example:
type mySlice []int func (s mySlice) Equal(b any) bool { if s2, ok := b.(mySlice); ok { if len(s) == len(s2) { for i, v := range s2 { if s[i] != v { return false } } return true } } return false }
type Getter ¶ added in v0.1.2
type Getter[K comparable, V any] interface { Get(k K) (v V) }
Getter gets a value from a map.
type Loader ¶ added in v0.1.2
type Loader[K comparable, V any] interface { Load(k K) (v V, ok bool) }
Loader loads a value from a map.
type Map ¶
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Map is a go map that uses a standard set of functions shared with other Map-like types.
The recommended way to create a Map is to first declare a concrete type alias, and then call new on it, like this:
type MyMap = Map[string,int] m := new(MyMap)
This will allow you to swap in a different kind of Map just by changing the type.
func CollectMap ¶ added in v1.0.0
func CollectMap[K comparable, V any](seq iter.Seq2[K, V]) *Map[K, V]
CollectMap collects key-value pairs from seq into a new Map and returns it.
Example ¶
m1 := StdMap[string, int]{"a": 1, "b": 2, "c": 3} m2 := CollectMap(m1.All()) fmt.Println(m2.String())
Output: {"a":1, "b":2, "c":3}
func NewMap ¶ added in v1.0.0
func NewMap[K comparable, V any](sources ...map[K]V) *Map[K, V]
NewMap creates a new map that maps values of type K to values of type V. Pass in zero or more standard maps and the contents of those maps will be copied to the new Map.
func (*Map[K, V]) Clone ¶ added in v1.0.0
Clone returns a copy of the Map. This is a shallow clone: the new keys and values are set using ordinary assignment.
func (*Map[K, V]) Copy ¶ added in v1.0.0
Copy copies the items from in to the map, overwriting any conflicting keys.
func (Map[K, V]) Delete ¶
func (m Map[K, V]) Delete(k K) V
Delete removes the key from the map. If the key does not exist, nothing happens.
func (*Map[K, V]) DeleteFunc ¶ added in v1.0.0
DeleteFunc deletes any key/value pairs for which del returns true.
func (*Map[K, V]) Equal ¶
Equal returns true if all the keys and values are equal.
If the values are not comparable, you should implement the Equaler interface on the values. Otherwise, you will get a runtime panic.
func (*Map[K, V]) Get ¶
func (m *Map[K, V]) Get(k K) V
Get returns the value for the given key. If the key does not exist, the zero value will be returned.
func (*Map[K, V]) Insert ¶ added in v1.0.0
Insert adds the values from seq to the map. Duplicate keys are overridden.
func (*Map[K, V]) Keys ¶
func (m *Map[K, V]) Keys() []K
Keys returns a new slice containing the keys of the map.
func (*Map[K, V]) KeysIter ¶ added in v1.0.0
KeysIter returns an iterator over all the keys in the map.
func (*Map[K, V]) Load ¶
Load returns the value based on its key, and a boolean indicating whether it exists in the map. This is the same interface as sync.Map.Load()
func (*Map[K, V]) MarshalBinary ¶
MarshalBinary implements the BinaryMarshaler interface to convert the map to a byte stream.
func (*Map[K, V]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.
func (*Map[K, V]) Merge ¶
Merge copies the items from in to the map, overwriting any conflicting keys. Deprecated: Call Copy instead.
func (*Map[K, V]) Range ¶
Range calls the given function for each key,value pair in the map. This is the same interface as sync.Map.Range(). While its safe to call methods of the map from within the Range function, its discouraged. If you ever switch to one of the SafeMap maps, it will cause a deadlock.
func (*Map[K, V]) String ¶
String returns the map as a string.
Example ¶
m := new(Map[string, int]) m.Set("a", 1) m.Set("b", 2) fmt.Print(m)
Output: {"a":1, "b":2}
func (*Map[K, V]) UnmarshalBinary ¶
UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a Map.
Note that you may need to register the map at init time with gob like this:
func init() { gob.Register(new(Map[keytype,valuetype])) }
func (*Map[K, V]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a Map. The JSON must start with an object.
func (*Map[K, V]) Values ¶
func (m *Map[K, V]) Values() []V
Values returns a new slice containing the values of the map.
func (*Map[K, V]) ValuesIter ¶ added in v1.0.0
ValuesIter returns an iterator over all the values in the map.
type MapI ¶
type MapI[K comparable, V any] interface { Setter[K, V] Getter[K, V] Loader[K, V] Clear() Len() int Range(func(k K, v V) bool) Has(k K) bool Keys() []K Values() []V Merge(MapI[K, V]) Equal(MapI[K, V]) bool Delete(k K) V All() iter.Seq2[K, V] KeysIter() iter.Seq[K] ValuesIter() iter.Seq[V] Insert(seq iter.Seq2[K, V]) DeleteFunc(del func(K, V) bool) String() string }
MapI is the interface used by all the Map types.
type OrderedSet ¶ added in v1.1.0
OrderedSet implements a set of values that will be returned sorted.
Ordered sets are useful when in general you don't care about ordering, but you would still like the same values to be presented in the same order when they are asked for. Examples include test code, iterators, values stored in a database, or values that will be presented to a user.
func NewOrderedSet ¶ added in v1.1.0
func NewOrderedSet[K cmp.Ordered](values ...K) *OrderedSet[K]
func (*OrderedSet[K]) Add ¶ added in v1.1.1
func (m *OrderedSet[K]) Add(k ...K) SetI[K]
Add adds the value to the set. If the value already exists, nothing changes.
func (*OrderedSet[K]) All ¶ added in v1.1.0
func (m *OrderedSet[K]) All() iter.Seq[K]
All returns an iterator over all the items in the set. Order is determinate.
func (*OrderedSet[K]) Clear ¶ added in v1.1.2
func (m *OrderedSet[K]) Clear()
Clear resets the set to an empty set
func (*OrderedSet[K]) Clone ¶ added in v1.1.1
func (m *OrderedSet[K]) Clone() *OrderedSet[K]
Clone returns a copy of the Set. This is a shallow clone: the new keys and values are set using ordinary assignment.
func (*OrderedSet[K]) Copy ¶ added in v1.1.2
func (m *OrderedSet[K]) Copy(in SetI[K])
Copy adds the values from in to the set.
func (*OrderedSet[K]) Delete ¶ added in v1.1.2
func (m *OrderedSet[K]) Delete(k K)
Delete removes the value from the set. If the value does not exist, nothing happens.
func (*OrderedSet[K]) DeleteFunc ¶ added in v1.1.2
func (m *OrderedSet[K]) DeleteFunc(del func(K) bool)
DeleteFunc deletes any values for which del returns true.
func (*OrderedSet[K]) Equal ¶ added in v1.1.2
func (m *OrderedSet[K]) Equal(m2 SetI[K]) bool
Equal returns true if the two sets are the same length and contain the same values.
func (*OrderedSet[K]) Has ¶ added in v1.1.2
func (m *OrderedSet[K]) Has(k K) bool
Has returns true if the value exists in the set.
func (*OrderedSet[K]) Insert ¶ added in v1.1.2
func (m *OrderedSet[K]) Insert(seq iter.Seq[K])
Insert adds the values from seq to the map. Duplicates are overridden.
func (*OrderedSet[K]) Len ¶ added in v1.1.2
func (m *OrderedSet[K]) Len() int
Len returns the number of items in the set
func (*OrderedSet[K]) MarshalJSON ¶ added in v1.1.0
func (m *OrderedSet[K]) MarshalJSON() (out []byte, err error)
MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.
func (*OrderedSet[K]) Range ¶ added in v1.1.0
func (m *OrderedSet[K]) Range(f func(k K) bool)
Range will range over the values in order.
func (*OrderedSet[K]) String ¶ added in v1.1.2
func (m *OrderedSet[K]) String() string
String returns the set as a string.
Example ¶
m := NewOrderedSet("a", "c", "a", "b") fmt.Print(m.String())
Output: {"a","b","c"}
func (*OrderedSet[K]) Values ¶ added in v1.1.0
func (m *OrderedSet[K]) Values() []K
Values returns a new slice containing the values of the set.
type SafeMap ¶
type SafeMap[K comparable, V any] struct { sync.RWMutex // contains filtered or unexported fields }
SafeMap is a go map that is safe for concurrent use and that uses a standard set of functions shared with other Map-like types.
The recommended way to create a SafeMap is to first declare a concrete type alias, and then call new on it, like this:
type MyMap = SafeMap[string,int] m := new(MyMap)
This will allow you to swap in a different kind of Map just by changing the type.
Do not make a copy of a SafeMap using the equality operator (=). Use Clone instead.
func CollectSafeMap ¶ added in v1.0.0
func CollectSafeMap[K comparable, V any](seq iter.Seq2[K, V]) *SafeMap[K, V]
CollectSafeMap collects key-value pairs from seq into a new SafeMap and returns it.
func NewSafeMap ¶ added in v1.0.0
func NewSafeMap[K comparable, V any](sources ...map[K]V) *SafeMap[K, V]
NewSafeMap creates a new SafeMap. Pass in zero or more standard maps and the contents of those maps will be copied to the new SafeMap.
func (*SafeMap[K, V]) All ¶ added in v1.0.0
All returns an iterator over all the items in the map. This will lock the map, so care must be taken that the iterator does not call back functions in SafeMap which will also require a lock.
func (*SafeMap[K, V]) Clone ¶ added in v1.0.0
Clone returns a copy of the SafeMap. This is a shallow clone: the new keys and values are set using ordinary assignment.
func (*SafeMap[K, V]) Copy ¶ added in v1.0.0
Copy copies the keys and values of in into this map, overwriting any duplicates.
func (*SafeMap[K, V]) Delete ¶
func (m *SafeMap[K, V]) Delete(k K) (v V)
Delete removes the key from the map and returns the value. If the key does not exist, the zero value will be returned.
func (*SafeMap[K, V]) DeleteFunc ¶ added in v1.0.0
DeleteFunc deletes any key/value pairs for which del returns true.
func (*SafeMap[K, V]) Equal ¶
Equal returns true if all the keys in the given map exist in this map, and the values are the same
func (*SafeMap[K, V]) Get ¶
func (m *SafeMap[K, V]) Get(k K) (v V)
Get returns the value based on its key. If it does not exist, an empty string will be returned.
func (*SafeMap[K, V]) Insert ¶ added in v1.0.0
Insert adds the values from seq to the map. Duplicate keys are overridden.
func (*SafeMap[K, V]) Keys ¶
func (m *SafeMap[K, V]) Keys() (keys []K)
Keys returns a slice of the keys. It will return a nil slice if the map is empty. Multiple calls to Keys will result in the same list of keys, but may be in a different order.
func (*SafeMap[K, V]) KeysIter ¶ added in v1.0.0
KeysIter returns an iterator over all the keys in the map. This will lock the map, so care must be taken that the iterator does not call back functions in SafeMap which will also require a lock.
func (*SafeMap[K, V]) Load ¶
Load returns the value based on its key, and a boolean indicating whether it exists in the map. This is the same interface as sync.Map.Load().
func (*SafeMap[K, V]) MarshalBinary ¶
MarshalBinary implements the BinaryMarshaler interface to convert the map to a byte stream.
func (*SafeMap[K, V]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.
func (*SafeMap[K, V]) Merge ¶
Merge merges the given map with the current one. The given one takes precedent on collisions. Deprecated: Use Copy instead.
func (*SafeMap[K, V]) Range ¶
Range will call the given function with every key and value in the map. If f returns false, it stops the iteration. This pattern is taken from sync.Map. During this process, the map will be locked, so do not pass a function that will take significant amounts of time, nor will call into other methods of the SafeMap which might also need a lock.
func (*SafeMap[K, V]) Set ¶
func (m *SafeMap[K, V]) Set(k K, v V)
Set sets the key to the given value.
func (*SafeMap[K, V]) String ¶
String outputs the map as a string.
Example ¶
m := new(SafeMap[string, int]) m.Set("a", 1) m.Set("b", 2) fmt.Print(m)
Output: {"a":1, "b":2}
func (*SafeMap[K, V]) UnmarshalBinary ¶
UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a SafeMap.
func (*SafeMap[K, V]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a SafeMap. The JSON must start with an object.
func (*SafeMap[K, V]) Values ¶
func (m *SafeMap[K, V]) Values() (v []V)
Values returns a slice of the values. It will return a nil slice if the map is empty. Multiple calls to Values will result in the same list of values, but may be in a different order.
func (*SafeMap[K, V]) ValuesIter ¶ added in v1.0.0
ValuesIter returns an iterator over all the values in the map. During this process, the map will be locked, so care must be taken that iterator will not attempt to call other functions in SafeMap which also need a lock.
type SafeSliceMap ¶
type SafeSliceMap[K comparable, V any] struct { sync.RWMutex // contains filtered or unexported fields }
SafeSliceMap is a go map that uses a slice to save the order of its keys so that the map can be ranged in a predictable order. SafeSliceMap is safe for concurrent use.
By default, the order will be the same order that items were inserted, i.e. a FIFO list, which is similar to how PHP arrays work. You can also define a sort function on the list to keep it sorted.
The recommended way to create a SliceMap is to first declare a concrete type alias, and then call new on it, like this:
type MyMap = SafeSliceMap[string,int] m := new(MyMap)
This will allow you to swap in a different kind of Map just by changing the type.
Call SetSortFunc to give the map a function that will keep the keys sorted in a particular order.
Do not make a copy of a SafeSliceMap using the equality operator. Use Clone() instead.
func CollectSafeSliceMap ¶ added in v1.0.0
func CollectSafeSliceMap[K comparable, V any](seq iter.Seq2[K, V]) *SafeSliceMap[K, V]
CollectSafeSliceMap collects key-value pairs from seq into a new SafeSliceMap and returns it.
func NewSafeSliceMap ¶ added in v1.0.0
func NewSafeSliceMap[K comparable, V any](sources ...map[K]V) *SafeSliceMap[K, V]
NewSafeSliceMap creates a new SafeSliceMap. Pass in zero or more standard maps and the contents of those maps will be copied to the new SafeSliceMap.
func (*SafeSliceMap[K, V]) All ¶ added in v1.0.0
func (m *SafeSliceMap[K, V]) All() iter.Seq2[K, V]
All returns an iterator over all the items in the map in the order they were entered or sorted.
func (*SafeSliceMap[K, V]) Clear ¶
func (m *SafeSliceMap[K, V]) Clear()
Clear removes all the items in the map.
func (*SafeSliceMap[K, V]) Clone ¶ added in v1.0.0
func (m *SafeSliceMap[K, V]) Clone() *SafeSliceMap[K, V]
Clone returns a copy of the SafeSliceMap. This is a shallow clone of the keys and values: the new keys and values are set using ordinary assignment. The order is preserved.
func (*SafeSliceMap[K, V]) Copy ¶ added in v1.0.0
func (m *SafeSliceMap[K, V]) Copy(in MapI[K, V])
Copy will copy the given map into the current one.
func (*SafeSliceMap[K, V]) Delete ¶
func (m *SafeSliceMap[K, V]) Delete(key K) (val V)
Delete removes the item with the given key and returns the value.
func (*SafeSliceMap[K, V]) DeleteFunc ¶ added in v1.0.0
func (m *SafeSliceMap[K, V]) DeleteFunc(del func(K, V) bool)
DeleteFunc deletes any key/value pairs for which del returns true. Items are ranged in order. This function locks the entire slice structure for the entirety of the call, so be careful to avoid deadlocks when calling this on a very big structure.
func (*SafeSliceMap[K, V]) Equal ¶
func (m *SafeSliceMap[K, V]) Equal(m2 MapI[K, V]) bool
Equal returns true if all the keys and values are equal, regardless of the order.
If the values are not comparable, you should implement the Equaler interface on the values. Otherwise, you will get a runtime panic.
func (*SafeSliceMap[K, V]) Get ¶
func (m *SafeSliceMap[K, V]) Get(key K) (val V)
Get returns the value based on its key. If the key does not exist, an empty value is returned.
func (*SafeSliceMap[K, V]) GetAt ¶
func (m *SafeSliceMap[K, V]) GetAt(position int) (val V)
GetAt returns the value based on its position. If the position is out of bounds, an empty value is returned.
Example ¶
m := new(SafeSliceMap[string, int]) m.Set("b", 2) m.Set("c", 3) m.Set("a", 1) v := m.GetAt(1) fmt.Print(v)
Output: 3
func (*SafeSliceMap[K, V]) GetKeyAt ¶
func (m *SafeSliceMap[K, V]) GetKeyAt(position int) (key K)
GetKeyAt returns the key based on its position. If the position is out of bounds, an empty value is returned.
Example ¶
m := new(SafeSliceMap[string, int]) m.Set("b", 2) m.Set("c", 3) m.Set("a", 1) v := m.GetKeyAt(1) fmt.Print(v)
Output: c
func (*SafeSliceMap[K, V]) Has ¶
func (m *SafeSliceMap[K, V]) Has(key K) (ok bool)
Has returns true if the given key exists in the map.
func (*SafeSliceMap[K, V]) Insert ¶ added in v1.0.0
func (m *SafeSliceMap[K, V]) Insert(seq iter.Seq2[K, V])
Insert adds the values from seq to the end of the map. Duplicate keys are overridden but not moved. Will lock and unlock for each item in seq to give time to other go routines.
func (*SafeSliceMap[K, V]) Keys ¶
func (m *SafeSliceMap[K, V]) Keys() (keys []K)
Keys returns the keys of the map, in the order they were added or sorted.
func (*SafeSliceMap[K, V]) KeysIter ¶ added in v1.0.0
func (m *SafeSliceMap[K, V]) KeysIter() iter.Seq[K]
KeysIter returns an iterator over all the keys in the map. During this process, the map will be locked, so do not pass a function that will take significant amounts of time, nor will call into other methods of the SafeSliceMap which might also need a lock.
func (*SafeSliceMap[K, V]) Len ¶
func (m *SafeSliceMap[K, V]) Len() int
Len returns the number of items in the map.
func (*SafeSliceMap[K, V]) Load ¶
func (m *SafeSliceMap[K, V]) Load(key K) (val V, ok bool)
Load returns the value based on its key, and a boolean indicating whether it exists in the map. This is the same interface as sync.Map.Load()
func (*SafeSliceMap[K, V]) MarshalBinary ¶
func (m *SafeSliceMap[K, V]) MarshalBinary() (data []byte, err error)
MarshalBinary implements the BinaryMarshaler interface to convert the map to a byte stream. If you are using a sort function, you must save and restore the sort function in a separate operation since functions are not serializable.
func (*SafeSliceMap[K, V]) MarshalJSON ¶
func (m *SafeSliceMap[K, V]) MarshalJSON() (data []byte, err error)
MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.
func (*SafeSliceMap[K, V]) Merge ¶
func (m *SafeSliceMap[K, V]) Merge(in MapI[K, V])
Merge the given map into the current one. Deprecated: Use copy instead.
func (*SafeSliceMap[K, V]) Range ¶
func (m *SafeSliceMap[K, V]) Range(f func(key K, value V) bool)
Range will call the given function with every key and value in the order they were placed in the map, or in if you sorted the map, in your custom order. If f returns false, it stops the iteration. This pattern is taken from sync.Map. During this process, the map will be locked, so do not pass a function that will take significant amounts of time, nor will call into other methods of the SafeSliceMap which might also need a lock. The workaround is to call Keys() and iterate over the returned copy of the keys, but making sure your function can handle the situation where the key no longer exists in the slice.
func (*SafeSliceMap[K, V]) Set ¶
func (m *SafeSliceMap[K, V]) Set(key K, val V)
Set sets the given key to the given value.
If the key already exists, the range order will not change. If you want the order to change, call Delete first, and then Set.
func (*SafeSliceMap[K, V]) SetAt ¶
func (m *SafeSliceMap[K, V]) SetAt(index int, key K, val V)
SetAt sets the given key to the given value, but also inserts it at the index specified. If the index is bigger than the length, it puts it at the end. Negative indexes are backwards from the end.
Example ¶
m := new(SafeSliceMap[string, int]) m.Set("b", 2) m.Set("a", 1) m.SetAt(1, "c", 3) fmt.Println(m)
Output: {"b":2,"c":3,"a":1}
func (*SafeSliceMap[K, V]) SetSortFunc ¶
func (m *SafeSliceMap[K, V]) SetSortFunc(f func(key1, key2 K, val1, val2 V) bool)
SetSortFunc sets the sort function which will determine the order of the items in the map on an ongoing basis. Normally, items will iterate in the order they were added. The sort function is a Less function, that returns true when item 1 is "less" than item 2. The sort function receives both the keys and values, so it can use either or both to decide how to sort.
Example ¶
m := new(SafeSliceMap[string, int]) m.Set("b", 2) m.Set("a", 1) // This will print in the order items were assigned fmt.Println(m) // sort by keys m.SetSortFunc(func(k1, k2 string, v1, v2 int) bool { return k1 < k2 }) fmt.Println(m)
Output: {"b":2,"a":1} {"a":1,"b":2}
func (*SafeSliceMap[K, V]) String ¶
func (m *SafeSliceMap[K, V]) String() string
String outputs the map as a string.
func (*SafeSliceMap[K, V]) UnmarshalBinary ¶
func (m *SafeSliceMap[K, V]) UnmarshalBinary(data []byte) (err error)
UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a SafeSliceMap.
func (*SafeSliceMap[K, V]) UnmarshalJSON ¶
func (m *SafeSliceMap[K, V]) UnmarshalJSON(data []byte) (err error)
UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a Map. The JSON must start with an object.
func (*SafeSliceMap[K, V]) Values ¶
func (m *SafeSliceMap[K, V]) Values() (values []V)
Values returns a slice of the values in the order they were added or sorted.
func (*SafeSliceMap[K, V]) ValuesIter ¶ added in v1.0.0
func (m *SafeSliceMap[K, V]) ValuesIter() iter.Seq[V]
ValuesIter returns an iterator over all the values in the map. During this process, the map will be locked, so do not pass a function that will take significant amounts of time, nor will call into other methods of the SafeSliceMap which might also need a lock.
type Set ¶ added in v0.1.3
type Set[K comparable] struct { // contains filtered or unexported fields }
Set is a collection that keeps track of membership.
The recommended way to create a Set is to first declare a concrete type alias, and then call new on it, like this:
type MySet = Set[string] s := new(MySet)
This will allow you to swap in a different kind of Set just by changing the type.
func CollectSet ¶ added in v1.0.0
func CollectSet[K comparable](seq iter.Seq[K]) *Set[K]
CollectSet collects values from seq into a new Set and returns it.
func NewSet ¶ added in v1.0.0
func NewSet[K comparable](values ...K) *Set[K]
func (*Set[K]) Add ¶ added in v0.1.3
Add adds the value to the set. If the value already exists, nothing changes.
func (*Set[K]) All ¶ added in v1.0.0
All returns an iterator over all the items in the set. Order is not determinate.
func (*Set[K]) Clear ¶ added in v0.1.3
func (m *Set[K]) Clear()
Clear resets the set to an empty set
func (*Set[K]) Clone ¶ added in v1.0.0
Clone returns a copy of the Set. This is a shallow clone: the new keys and values are set using ordinary assignment.
func (*Set[K]) Delete ¶ added in v0.1.3
func (m *Set[K]) Delete(k K)
Delete removes the value from the set. If the value does not exist, nothing happens.
func (*Set[K]) DeleteFunc ¶ added in v1.0.0
DeleteFunc deletes any values for which del returns true.
func (*Set[K]) Equal ¶ added in v0.1.3
Equal returns true if the two sets are the same length and contain the same values.
func (*Set[K]) Insert ¶ added in v1.0.0
Insert adds the values from seq to the map. Duplicates are overridden.
func (*Set[K]) MarshalBinary ¶ added in v0.1.3
MarshalBinary implements the BinaryMarshaler interface to convert the set to a byte stream.
func (*Set[K]) MarshalJSON ¶ added in v0.1.3
MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.
func (*Set[K]) Merge ¶ added in v0.1.3
Merge adds the values from the given set to the set. Deprecated: Call Copy instead.
func (*Set[K]) Range ¶ added in v0.1.3
Range calls the given function for each member in the set. The function should return true to continue ranging, or false to stop. While its safe to call methods of the set from within the Range function, its discouraged. If you ever switch to one of the SafeSet sets, it will cause a deadlock.
func (*Set[K]) String ¶ added in v0.1.3
String returns the set as a string.
Example ¶
m := new(Set[string]) m.Add("a") fmt.Print(m.String())
Output: {"a"}
func (*Set[K]) UnmarshalBinary ¶ added in v0.1.3
UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a Set.
Note that you may need to register the set at init time with gob like this:
func init() { gob.Register(new(Set[keytype])) }
func (*Set[K]) UnmarshalJSON ¶ added in v0.1.3
UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a Set. The JSON must start with a list.
type SetI ¶ added in v0.1.3
type SetI[K comparable] interface { Add(k ...K) SetI[K] Clear() Len() int Copy(in SetI[K]) Range(func(k K) bool) Has(k K) bool Values() []K // Deprecated: use Copy instead Merge(SetI[K]) Equal(SetI[K]) bool Delete(k K) All() iter.Seq[K] Insert(seq iter.Seq[K]) DeleteFunc(del func(K) bool) }
SetI is the interface used by all the Set types.
type Setter ¶ added in v0.1.2
type Setter[K comparable, V any] interface { Set(K, V) }
Setter sets a value in a map.
type SliceMap ¶
type SliceMap[K comparable, V any] struct { // contains filtered or unexported fields }
SliceMap is a go map that uses a slice to save the order of its keys so that the map can be ranged in a predictable order. By default, the order will be the same order that items were inserted, i.e. a FIFO list, which is similar to how PHP arrays work. You can also define a sort function on the list to keep it sorted.
The recommended way to create a SliceMap is to first declare a concrete type alias, and then call new on it, like this:
type MyMap = SliceMap[string,int] m := new(MyMap)
This will allow you to swap in a different kind of Map just by changing the type.
Call SetSortFunc to give the map a function that will keep the keys sorted in a particular order.
func CollectSliceMap ¶ added in v1.0.0
func CollectSliceMap[K comparable, V any](seq iter.Seq2[K, V]) *SliceMap[K, V]
CollectSliceMap collects key-value pairs from seq into a new SliceMap and returns it.
func NewSliceMap ¶ added in v1.0.0
func NewSliceMap[K comparable, V any](sources ...map[K]V) *SliceMap[K, V]
NewSliceMap creates a new SliceMap. Pass in zero or more standard maps and the contents of those maps will be copied to the new SafeMap.
func (*SliceMap[K, V]) All ¶ added in v1.0.0
All returns an iterator over all the items in the map in the order they were entered or sorted.
func (*SliceMap[K, V]) Clear ¶
func (m *SliceMap[K, V]) Clear()
Clear removes all the items in the map.
func (*SliceMap[K, V]) Clone ¶ added in v1.0.0
Clone returns a copy of the SliceMap. This is a shallow clone of the keys and values: the new keys and values are set using ordinary assignment. The order is preserved.
func (*SliceMap[K, V]) Copy ¶ added in v1.0.0
Copy copies the keys and values of in into the current one. Duplicate keys will have the values replaced, but not the order.
func (*SliceMap[K, V]) Delete ¶
func (m *SliceMap[K, V]) Delete(key K) (val V)
Delete removes the key from the map and returns the value. If the key does not exist, the zero value will be returned.
func (*SliceMap[K, V]) DeleteFunc ¶ added in v1.0.0
DeleteFunc deletes any key/value pairs for which del returns true. Items are ranged in order.
func (*SliceMap[K, V]) Equal ¶
Equal returns true if all the keys and values are equal, regardless of the order.
If the values are not comparable, you should implement the Equaler interface on the values. Otherwise, you will get a runtime panic.
func (*SliceMap[K, V]) Get ¶
func (m *SliceMap[K, V]) Get(key K) (val V)
Get returns the value based on its key. If the key does not exist, an empty value is returned.
func (*SliceMap[K, V]) GetAt ¶
GetAt returns the value based on its position. If the position is out of bounds, an empty value is returned.
Example ¶
m := new(SliceMap[string, int]) m.Set("b", 2) m.Set("c", 3) m.Set("a", 1) v := m.GetAt(1) fmt.Print(v)
Output: 3
func (*SliceMap[K, V]) GetKeyAt ¶
GetKeyAt returns the key based on its position. If the position is out of bounds, an empty value is returned.
Example ¶
m := new(SliceMap[string, int]) m.Set("b", 2) m.Set("c", 3) m.Set("a", 1) v := m.GetKeyAt(1) fmt.Print(v)
Output: c
func (*SliceMap[K, V]) Insert ¶ added in v1.0.0
Insert adds the values from seq to the end of the map. Duplicate keys are overridden but not moved.
func (*SliceMap[K, V]) Keys ¶
func (m *SliceMap[K, V]) Keys() (keys []K)
Keys returns a new slice of the keys of the map, in the order they were added or sorted
func (*SliceMap[K, V]) KeysIter ¶ added in v1.0.0
KeysIter returns an iterator over all the keys in the map.
func (*SliceMap[K, V]) Load ¶
Load returns the value based on its key, and a boolean indicating whether it exists in the map. This is the same interface as sync.StdMap.Load()
func (*SliceMap[K, V]) MarshalBinary ¶
MarshalBinary implements the BinaryMarshaler interface to convert the map to a byte stream. If you are using a sort function, you must save and restore the sort function in a separate operation since functions are not serializable.
func (*SliceMap[K, V]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.
func (*SliceMap[K, V]) Merge ¶
Merge the given map into the current one. Deprecated: use Copy instead.
func (*SliceMap[K, V]) Range ¶
Range will call the given function with every key and value in the order they were placed in the map, or in if you sorted the map, in your custom order. If f returns false, it stops the iteration. This pattern is taken from sync.Map.
func (*SliceMap[K, V]) Set ¶
func (m *SliceMap[K, V]) Set(key K, val V)
Set sets the given key to the given value.
If the key already exists, the range order will not change. If you want the order to change, call Delete first, and then Set.
func (*SliceMap[K, V]) SetAt ¶
SetAt sets the given key to the given value, but also inserts it at the index specified. If the index is bigger than the length, it puts it at the end. Negative indexes are backwards from the end.
Example ¶
m := new(SliceMap[string, int]) m.Set("b", 2) m.Set("a", 1) m.SetAt(1, "c", 3) fmt.Println(m)
Output: {"b":2,"c":3,"a":1}
func (*SliceMap[K, V]) SetSortFunc ¶
SetSortFunc sets the sort function which will determine the order of the items in the map on an ongoing basis. Normally, items will iterate in the order they were added.
When you call SetSortFunc, the map keys will be sorted. To turn off sorting, set the sort function to nil.
The sort function is a Less function, that returns true when item 1 is "less" than item 2. The sort function receives both the keys and values, so it can use either or both to decide how to sort.
Example ¶
m := new(SliceMap[string, int]) m.Set("b", 2) m.Set("a", 1) // This will print in the order items were assigned fmt.Println(m) // sort by keys m.SetSortFunc(func(k1, k2 string, v1, v2 int) bool { return k1 < k2 }) fmt.Println(m)
Output: {"b":2,"a":1} {"a":1,"b":2}
func (*SliceMap[K, V]) UnmarshalBinary ¶
UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a SliceMap.
func (*SliceMap[K, V]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a SliceMap. The JSON must start with an object.
func (*SliceMap[K, V]) Values ¶
func (m *SliceMap[K, V]) Values() (values []V)
Values returns a slice of the values in the order they were added or sorted.
func (*SliceMap[K, V]) ValuesIter ¶ added in v1.0.0
ValuesIter returns an iterator over all the values in the map.
type SliceSet ¶ added in v1.2.0
type SliceSet[K comparable] struct { // contains filtered or unexported fields }
SliceSet implements a set of values that will be returned in the order added, or based on a sorting function.
The differences between SliceSet and OrderedSet are:
- A SliceSet can hold comparable types, vs. OrderedSet can only hold cmp.Ordered types.
- A SliceSet sorts whenever items are added, while OrderedSet only sorts when the order is asked for.
SliceSet is built on top of SliceMap.
func NewSliceSet ¶ added in v1.2.0
func NewSliceSet[K comparable](values ...K) *SliceSet[K]
func (*SliceSet[K]) Add ¶ added in v1.2.0
Add adds the value to the set. If the value already exists, nothing changes.
func (*SliceSet[K]) All ¶ added in v1.2.0
All returns an iterator over all the items in the set. Order is determinate.
func (*SliceSet[K]) Clear ¶ added in v1.2.0
func (m *SliceSet[K]) Clear()
Clear resets the set to an empty set
func (*SliceSet[K]) Clone ¶ added in v1.2.0
Clone returns a copy of the Set. This is a shallow clone: the new keys and values are set using ordinary assignment.
func (*SliceSet[K]) Delete ¶ added in v1.2.0
func (m *SliceSet[K]) Delete(k K)
Delete removes the value from the set. If the value does not exist, nothing happens.
func (*SliceSet[K]) DeleteFunc ¶ added in v1.2.0
DeleteFunc deletes any values for which del returns true.
func (*SliceSet[K]) Equal ¶ added in v1.2.0
Equal returns true if the two sets are the same length and contain the same values.
func (*SliceSet[K]) Insert ¶ added in v1.2.0
Insert adds the values from seq to the map. Duplicates are overridden.
func (*SliceSet[K]) MarshalBinary ¶ added in v1.2.0
MarshalBinary implements the BinaryMarshaler interface to convert the set to a byte stream.
func (*SliceSet[K]) MarshalJSON ¶ added in v1.2.0
MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.
func (*SliceSet[K]) Merge ¶ added in v1.2.0
Merge adds the values from the given set to the set. Deprecated: Call Copy instead.
func (*SliceSet[K]) SetSortFunc ¶ added in v1.2.0
SetSortFunc sets the sort function which will determine the order of the items in the set on an ongoing basis. Normally, items will iterate in the order they were added.
When you call SetSortFunc, the values will be sorted. To turn off sorting, set the sort function to nil.
The sort function is a Less function, that returns true when item 1 is "less" than item 2.
func (*SliceSet[K]) String ¶ added in v1.2.0
String returns the set as a string.
Example ¶
m := NewSliceSet("a", "c", "a", "b") fmt.Print(m.String())
Output: {"a","c","b"}
func (*SliceSet[K]) UnmarshalBinary ¶ added in v1.2.0
UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a Set.
Note that you may need to register the set at init time with gob like this:
func init() { gob.Register(new(Set[keytype])) }
func (*SliceSet[K]) UnmarshalJSON ¶ added in v1.2.0
UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a Set. The JSON must start with a list.
type StdMap ¶
type StdMap[K comparable, V any] map[K]V
StdMap wraps a standard go map with a standard set of functions shared with other MapI-like types.
The zero value is NOT settable. Use NewStdMap to create a new StdMap object, or use standard map instantiation syntax like this:
m := StdMap[string, int]{"a":1}
StdMap is mostly a convenience type for making a standard Go map into a MapI interface. Generally, you should use Map instead, as it presents a consistent interface that allows you to swap the underlying type without changing implemented code.
func Cast ¶
func Cast[M ~map[K]V, K comparable, V any](m M) StdMap[K, V]
Cast is a convenience method for casting a standard Go map to a StdMap type. Note that this is a cast, so the return value is the equivalent map of what was past in. Use this primarily to make a standard map into a MapI object.
Example ¶
m := map[string]int{"a": 1} b := Cast(m) fmt.Print(b.Len())
Output: 1
func CollectStdMap ¶ added in v1.0.0
func CollectStdMap[K comparable, V any](seq iter.Seq2[K, V]) StdMap[K, V]
CollectStdMap collects key-value pairs from seq into a new StdMap and returns it.
func NewStdMap ¶
func NewStdMap[K comparable, V any](sources ...map[K]V) StdMap[K, V]
NewStdMap creates a new map that maps values of type K to values of type V. Pass in zero or more standard maps and the contents of those maps will be copied to the new StdMap. You can also create a new StdMap like this:
m := StdMap[string, int]{"a":1}
func (StdMap[K, V]) All ¶ added in v1.0.0
All returns an iterator over all the items in the map.
Example ¶
m := StdMap[string, int]{"a": 1, "b": 2, "c": 3} var actualKeys []string var actualValues []int for k, v := range m.All() { actualKeys = append(actualKeys, k) actualValues = append(actualValues, v) } slices.Sort(actualKeys) slices.Sort(actualValues) fmt.Println(actualKeys) fmt.Println(actualValues)
Output: [a b c] [1 2 3]
func (StdMap[K, V]) Clone ¶ added in v1.0.0
Clone returns a copy of the StdMap. This is a shallow clone: the new keys and values are set using ordinary assignment.
func (StdMap[K, V]) Copy ¶ added in v1.0.0
Copy copies the items from in to the map, overwriting any conflicting keys.
func (StdMap[K, V]) Delete ¶
func (m StdMap[K, V]) Delete(k K) (v V)
Delete removes the key from the map and returns the value. If the key does not exist, the zero value will be returned.
func (StdMap[K, V]) DeleteFunc ¶ added in v1.0.0
DeleteFunc deletes any key/value pairs for which del returns true.
Example ¶
m1 := StdMap[string, int]{"a": 1, "b": 2, "c": 3} m1.DeleteFunc(func(k string, v int) bool { return v != 2 }) fmt.Println(m1.String())
Output: {"b":2}
func (StdMap[K, V]) Equal ¶
Equal returns true if all the keys and values are equal.
If the values are not comparable, you should implement the Equaler interface on the values. Otherwise, you will get a runtime panic.
func (StdMap[K, V]) Get ¶
func (m StdMap[K, V]) Get(k K) (v V)
Get returns the value for the given key. If the key does not exist, the zero value will be returned.
func (StdMap[K, V]) Insert ¶ added in v1.0.0
Insert adds the values from seq to the map. Duplicate keys are overridden.
func (StdMap[K, V]) Keys ¶
func (m StdMap[K, V]) Keys() (keys []K)
Keys returns a new slice containing the keys of the map.
func (StdMap[K, V]) KeysIter ¶ added in v1.0.0
KeysIter returns an iterator over all the keys in the map.
Example ¶
m := StdMap[string, int]{"a": 1, "b": 2, "c": 3} var actualKeys []string for k := range m.KeysIter() { actualKeys = append(actualKeys, k) } slices.Sort(actualKeys) fmt.Println(actualKeys)
Output: [a b c]
func (StdMap[K, V]) Load ¶
Load returns the value based on its key, and a boolean indicating whether it exists in the map. This is the same interface as sync.Map.Load()
func (StdMap[K, V]) MarshalBinary ¶
MarshalBinary implements the BinaryMarshaler interface to convert the map to a byte stream.
func (StdMap[K, V]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.
func (StdMap[K, V]) Merge ¶
Merge copies the items from in to the map, overwriting any conflicting keys. Deprecated: use Copy instead
func (StdMap[K, V]) Range ¶
Range calls the given function for each key,value pair in the map. This is the same interface as sync.Map.Range(). While its safe to call methods of the map from within the Range function, its discouraged. If you ever switch to one of the SafeMap maps, it will cause a deadlock.
You can also range over a map using All().
func (StdMap[K, V]) Set ¶
func (m StdMap[K, V]) Set(k K, v V)
Set sets the given key to the given value.
func (*StdMap[K, V]) UnmarshalBinary ¶
UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a Map.
Note that you will likely need to register the unmarshaller at init time with gob like this:
func init() { gob.Register(new(Map[K,V])) }
func (*StdMap[K, V]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a StdMap. The JSON must start with an object.
func (StdMap[K, V]) Values ¶
func (m StdMap[K, V]) Values() (values []V)
Values returns a new slice containing the values of the map.
func (StdMap[K, V]) ValuesIter ¶ added in v1.0.0
ValuesIter returns an iterator over all the values in the map.
Example ¶
m := StdMap[string, int]{"a": 1, "b": 2, "c": 3} var actualValues []int for v := range m.ValuesIter() { actualValues = append(actualValues, v) } slices.Sort(actualValues) fmt.Println(actualValues)
Output: [1 2 3]