Documentation
¶
Overview ¶
Package typeddict provides a generic, concurrent, type-safe dictionary. It acts as a wrapper around sync.Map to enforce that all values are of a specific type, preventing runtime type errors.
Index ¶
- func Delete[T any](m *TypedDict[T], key string)
- func Get[T any](dict *TypedDict[T], key string) (T, bool)
- func GetOrDefault[T any](m *TypedDict[T], key string, defaultValue T) T
- func GetOrSetFunc[T any](m *TypedDict[T], key string, genFunc func() T) T
- func Set[T any](m *TypedDict[T], key string, value T)
- type TypedDict
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Get ¶
Get retrieves a value from the dictionary for the given key. It returns the value and true if the key is found, otherwise it returns the zero value of type T and false. It panics if the stored value is not of the expected type T, which indicates a bug or misuse.
func GetOrDefault ¶
GetOrDefault retrieves a value for a key, or returns the provided default value if the key is not found.
func GetOrSetFunc ¶
GetOrSetFunc retrieves the value for a key, or if the key is not found, it generates a new value using the provided genFunc, stores it, and returns the new value. The operation is atomic for each key.
Types ¶
type TypedDict ¶
type TypedDict[T any] struct { // contains filtered or unexported fields }
TypedDict is a generic, concurrent, type-safe dictionary. It internally uses a sync.Map for the container and another for key-specific mutexes, ensuring thread-safe operations on individual keys.
func NewTypedDict ¶
NewTypedDict creates and returns a new, empty TypedDict.