maps

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2023 License: MIT Imports: 7 Imported by: 11

README

Go Reference Build Status Go Report Card codecov

maps

maps is a library using Go generics that offers a standard interface for manipulating different kinds of maps.

Using the same interface, you can create and use a standard Go map, a map that is safe for concurrency and/or a map that lets you order the keys in the map.

Example

package main

import . "github.com/goradd/maps"
import "fmt"

type myMap = Map[string,int] // the equal sign here is critical!
type myStdMap = StdMap[string, int]

func main() {
	m := new(Map[string, int])
	
	m.Merge(myStdMap{"b":2, "c":3})
	m.Set("a",1)

	sum := 0
	m.Range(func(k string, v int) bool {
		sum += v
		return true
    })
	fmt.Print(sum)
}

By simply changing myMap to a SafeMap, you can make the map safe for concurrent use. Or, you can change myMap to a SliceMap, or a SafeSliceMap to also be able to iterate the map in the order it was created, similar to a PHP map.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Equaler

type Equaler interface {
	Equal(a any) bool
}

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]) Clear

func (m *Map[K, V]) Clear()

Clear resets the map to an empty map

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

func (m Map[K, V]) Equal(m2 MapI[K, V]) bool

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]) Has

func (m Map[K, V]) Has(k K) bool

Has returns true if the key exists.

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]) Len

func (m Map[K, V]) Len() int

Len returns the number of items in the map

func (Map[K, V]) Load

func (m Map[K, V]) Load(k K) (V, 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 (Map[K, V]) MarshalBinary

func (m Map[K, V]) MarshalBinary() ([]byte, error)

MarshalBinary implements the BinaryMarshaler interface to convert the map to a byte stream.

func (Map[K, V]) MarshalJSON

func (m Map[K, V]) MarshalJSON() (out []byte, err error)

MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.

func (*Map[K, V]) Merge

func (m *Map[K, V]) Merge(in MapI[K, V])

Merge copies the items from in to the map, overwriting any conflicting keys.

func (Map[K, V]) Range

func (m Map[K, V]) Range(f func(k K, v V) bool)

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]) Set

func (m *Map[K, V]) Set(k K, v V)

Set sets the key to the given value.

func (Map[K, V]) String

func (m Map[K, V]) String() 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

func (m *Map[K, V]) UnmarshalBinary(data []byte) (err error)

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

func (m *Map[K, V]) UnmarshalJSON(in []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 (Map[K, V]) Values

func (m Map[K, V]) Values() []V

Values returns a new slice containing the values of 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)
}

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]) Clear

func (m *SafeMap[K, V]) Clear()

Clear resets the map to an empty map.

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

func (m *SafeMap[K, V]) Equal(m2 MapI[K, V]) bool

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]) Has

func (m *SafeMap[K, V]) Has(k K) (exists bool)

Has returns true if the given key exists in the map.

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]) Len

func (m *SafeMap[K, V]) Len() (l int)

Len returns the number of items in the map

func (*SafeMap[K, V]) Load

func (m *SafeMap[K, V]) Load(k K) (v 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 (*SafeMap[K, V]) MarshalBinary

func (m *SafeMap[K, V]) MarshalBinary() ([]byte, error)

MarshalBinary implements the BinaryMarshaler interface to convert the map to a byte stream.

func (*SafeMap[K, V]) MarshalJSON

func (m *SafeMap[K, V]) MarshalJSON() (out []byte, err error)

MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.

func (*SafeMap[K, V]) Merge

func (m *SafeMap[K, V]) Merge(in MapI[K, V])

Merge merges the given map with the current one. The given one takes precedent on collisions.

func (*SafeMap[K, V]) Range

func (m *SafeMap[K, V]) Range(f func(k K, v V) bool)

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

func (m *SafeMap[K, V]) String() 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

func (m *SafeMap[K, V]) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a SafeMap.

func (*SafeMap[K, V]) UnmarshalJSON

func (m *SafeMap[K, V]) UnmarshalJSON(in []byte) (err error)

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.

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

func (m *Set[K]) Add(k ...K) SetI[K]

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

func (m *Set[K]) Equal(m2 SetI[K]) bool

Equal returns true if the two sets are the same length and contain the same values.

func (*Set[K]) Has added in v0.1.3

func (m *Set[K]) Has(k K) bool

Has returns true if the value exists in the set.

func (*Set[K]) Len added in v0.1.3

func (m *Set[K]) Len() int

Len returns the number of items in the set

func (*Set[K]) MarshalBinary added in v0.1.3

func (m *Set[K]) MarshalBinary() ([]byte, error)

MarshalBinary implements the BinaryMarshaler interface to convert the set to a byte stream.

func (*Set[K]) MarshalJSON added in v0.1.3

func (m *Set[K]) MarshalJSON() (out []byte, err error)

MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.

func (*Set[K]) Merge added in v0.1.3

func (m *Set[K]) Merge(in SetI[K])

Merge adds the values from the given set to the set.

func (*Set[K]) Range added in v0.1.3

func (m *Set[K]) Range(f func(k K) bool)

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

func (m *Set[K]) String() string

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

func (m *Set[K]) UnmarshalBinary(data []byte) (err error)

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

func (m *Set[K]) UnmarshalJSON(in []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a Set. The JSON must start with a list.

func (*Set[K]) Values added in v0.1.3

func (m *Set[K]) Values() []K

Values returns a new slice containing the values of the set.

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

func (m *SliceMap[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 (*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

func (m *SliceMap[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(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

func (m *SliceMap[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(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]) Has

func (m *SliceMap[K, V]) Has(key K) (ok bool)

Has returns true if the given key exists in the map.

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]) Len

func (m *SliceMap[K, V]) Len() int

Len returns the number of items in the map

func (*SliceMap[K, V]) Load

func (m *SliceMap[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.StdMap.Load()

func (*SliceMap[K, V]) MarshalBinary

func (m *SliceMap[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 (*SliceMap[K, V]) MarshalJSON

func (m *SliceMap[K, V]) MarshalJSON() (data []byte, err error)

MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.

func (*SliceMap[K, V]) Merge

func (m *SliceMap[K, V]) Merge(in MapI[K, V])

Merge the given map into the current one.

func (*SliceMap[K, V]) Range

func (m *SliceMap[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 (*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

func (m *SliceMap[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(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

func (m *SliceMap[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.

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]) String

func (m *SliceMap[K, V]) String() string

String outputs the map as a string.

func (*SliceMap[K, V]) UnmarshalBinary

func (m *SliceMap[K, V]) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a SliceMap.

func (*SliceMap[K, V]) UnmarshalJSON

func (m *SliceMap[K, V]) UnmarshalJSON(data []byte) (err error)

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() (vals []V)

Values returns a slice of the values in the order they were added or sorted.

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]) Clear

func (m StdMap[K, V]) Clear()

Clear resets the map to an empty map

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

func (m StdMap[K, V]) Equal(m2 MapI[K, V]) bool

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]) Has

func (m StdMap[K, V]) Has(k K) (exists bool)

Has returns true if the key exists.

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]) Len

func (m StdMap[K, V]) Len() int

Len returns the number of items in the map.

func (StdMap[K, V]) Load

func (m StdMap[K, V]) Load(k K) (v 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 (StdMap[K, V]) MarshalBinary

func (m StdMap[K, V]) MarshalBinary() ([]byte, error)

MarshalBinary implements the BinaryMarshaler interface to convert the map to a byte stream.

func (StdMap[K, V]) MarshalJSON

func (m StdMap[K, V]) MarshalJSON() (out []byte, err error)

MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.

func (StdMap[K, V]) Merge

func (m StdMap[K, V]) Merge(in MapI[K, V])

Merge copies the items from in to the map, overwriting any conflicting keys.

func (StdMap[K, V]) Range

func (m StdMap[K, V]) Range(f func(k K, v V) bool)

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]) String

func (m StdMap[K, V]) String() string

String returns a string representation of the map.

func (*StdMap[K, V]) UnmarshalBinary

func (m *StdMap[K, V]) UnmarshalBinary(data []byte) (err error)

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

func (m *StdMap[K, V]) UnmarshalJSON(in []byte) (err error)

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.

Jump to

Keyboard shortcuts

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