Documentation ¶
Index ¶
- type Equaler
- type Getter
- type Loader
- type Map
- func (m *Map[K, V]) Clear()
- func (m Map[K, V]) Delete(k K)
- 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]) Keys() []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
- type MapI
- type SafeMap
- func (m *SafeMap[K, V]) Clear()
- func (m *SafeMap[K, V]) Delete(k K)
- 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]) Keys() (keys []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)
- type SafeSliceMap
- func (m *SafeSliceMap[K, V]) Clear()
- func (m *SafeSliceMap[K, V]) Delete(key K)
- 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]) Keys() (keys []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() (vals []V)
- type Set
- func (m *Set[K]) Add(k ...K) SetI[K]
- func (m *Set[K]) Clear()
- func (m *Set[K]) Delete(k K)
- func (m *Set[K]) Equal(m2 SetI[K]) bool
- func (m *Set[K]) Has(k K) bool
- 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]) Clear()
- func (m *SliceMap[K, V]) Delete(key K)
- 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]) Keys() (keys []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() (vals []V)
- type StdMap
- func (m StdMap[K, V]) Clear()
- func (m StdMap[K, V]) Delete(k K)
- 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]) Keys() (keys []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)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
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-comparible 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 (Map[K, V]) Delete ¶
func (m Map[K, V]) Delete(k K)
Delete removes the key from the map. If the key does not exist, nothing happens.
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]) Keys ¶
func (m Map[K, V]) Keys() []K
Keys returns a new slice containing the keys of 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.
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.
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) }
MapI is the interface used by all the Map types.
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.
func (*SafeMap[K, V]) Delete ¶
func (m *SafeMap[K, V]) Delete(k K)
Delete removes the key from the map. If the key does not exist, nothing happens.
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]) 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]) 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.
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.
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.
func (*SafeSliceMap[K, V]) Clear ¶
func (m *SafeSliceMap[K, V]) Clear()
Clear removes all the items in the map.
func (*SafeSliceMap[K, V]) Delete ¶
func (m *SafeSliceMap[K, V]) Delete(key K)
Delete removes the item with the given key.
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]) 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]) 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.
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.
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() (vals []V)
Values returns a slice of the values in the order they were added or sorted.
type Set ¶ added in v0.1.3
type Set[K comparable] struct { // contains filtered or unexported fields }
Set is a collection the 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 (*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]) Clear ¶ added in v0.1.3
func (m *Set[K]) Clear()
Clear resets the set to an empty set
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]) 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]) 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]) 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") m.Add("b") m.Add("a") v := m.Values() sort.Strings(v) fmt.Print(v)
Output: [a b]
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 Range(func(k K) bool) Has(k K) bool Values() []K Merge(SetI[K]) Equal(SetI[K]) bool Delete(k K) }
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 (*SliceMap[K, V]) Clear ¶
func (m *SliceMap[K, V]) Clear()
Clear removes all the items in the map.
func (*SliceMap[K, V]) Delete ¶
func (m *SliceMap[K, V]) Delete(key K)
Delete removes the item with the given key.
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]) Keys ¶
func (m *SliceMap[K, V]) Keys() (keys []K)
Keys returns the keys of the map, in the order they were added or sorted
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]) 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.
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 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]) Delete ¶
func (m StdMap[K, V]) Delete(k K)
Delete removes the key from the map. If the key does not exist, nothing happens.
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]) Keys ¶
func (m StdMap[K, V]) Keys() (keys []K)
Keys returns a new slice containing the keys of the map.
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.
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.
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.