10// Map is a concurrent map implementation that provides thread-safe access.
11type Map[K comparable, V any] struct {
16// NewMap creates a new thread-safe map with the specified key and value types.
17func NewMap[K comparable, V any]() *Map[K, V] {
23// NewMapFrom creates a new thread-safe map from an existing map.
24func NewMapFrom[K comparable, V any](m map[K]V) *Map[K, V] {
30// Set sets the value for the specified key in the map.
31func (m *Map[K, V]) Set(key K, value V) {
37// Del deletes the specified key from the map.
38func (m *Map[K, V]) Del(key K) {
44// Get gets the value for the specified key from the map.
45func (m *Map[K, V]) Get(key K) (V, bool) {
52// Len returns the number of items in the map.
53func (m *Map[K, V]) Len() int {
59// GetOrSet gets and returns the key if it exists, otherwise, it executes the
60// given function, set its return value for the given key, and returns it.
61func (m *Map[K, V]) GetOrSet(key K, fn func() V) V {
71// Take gets an item and then deletes it.
72func (m *Map[K, V]) Take(key K) (V, bool) {
80// Seq2 returns an iter.Seq2 that yields key-value pairs from the map.
81func (m *Map[K, V]) Seq2() iter.Seq2[K, V] {
84 maps.Copy(dst, m.inner)
86 return func(yield func(K, V) bool) {
87 for k, v := range dst {
95// Seq returns an iter.Seq that yields values from the map.
96func (m *Map[K, V]) Seq() iter.Seq[V] {
97 return func(yield func(V) bool) {
98 for _, v := range m.Seq2() {
107 _ json.Unmarshaler = &Map[string, any]{}
108 _ json.Marshaler = &Map[string, any]{}
111func (Map[K, V]) JSONSchemaAlias() any { //nolint
116// UnmarshalJSON implements json.Unmarshaler.
117func (m *Map[K, V]) UnmarshalJSON(data []byte) error {
120 m.inner = make(map[K]V)
121 return json.Unmarshal(data, &m.inner)
124// MarshalJSON implements json.Marshaler.
125func (m *Map[K, V]) MarshalJSON() ([]byte, error) {
128 return json.Marshal(m.inner)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list