typeddict

package
v0.60.1 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete[T any](m *TypedDict[T], key string)

Delete removes the key-value pair associated with the given key from the dictionary.

func Get

func Get[T any](dict *TypedDict[T], key string) (T, bool)

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

func GetOrDefault[T any](m *TypedDict[T], key string, defaultValue T) T

GetOrDefault retrieves a value for a key, or returns the provided default value if the key is not found.

func GetOrSetFunc

func GetOrSetFunc[T any](m *TypedDict[T], key string, genFunc func() T) T

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.

func Set

func Set[T any](m *TypedDict[T], key string, value T)

Set stores a key-value pair in the dictionary. If the key already exists, its value is overwritten.

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

func NewTypedDict[T any]() *TypedDict[T]

NewTypedDict creates and returns a new, empty TypedDict.

func (*TypedDict[T]) Keys

func (m *TypedDict[T]) Keys() []string

Keys returns all keys in the map as a slice of strings

Jump to

Keyboard shortcuts

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