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// NewLazyMap creates a new lazy-loaded map. The provided load function is
31// executed in a separate goroutine to populate the map.
32func NewLazyMap[K comparable, V any](load func() map[K]V) *Map[K, V] {
42// Reset replaces the inner map with the new one.
43func (m *Map[K, V]) Reset(input map[K]V) {
49// Set sets the value for the specified key in the map.
50func (m *Map[K, V]) Set(key K, value V) {
56// Del deletes the specified key from the map.
57func (m *Map[K, V]) Del(key K) {
63// Get gets the value for the specified key from the map.
64func (m *Map[K, V]) Get(key K) (V, bool) {
71// Len returns the number of items in the map.
72func (m *Map[K, V]) Len() int {
78// GetOrSet gets and returns the key if it exists, otherwise, it executes the
79// given function, set its return value for the given key, and returns it.
80func (m *Map[K, V]) GetOrSet(key K, fn func() V) V {
90// Take gets an item and then deletes it.
91func (m *Map[K, V]) Take(key K) (V, bool) {
99// Seq2 returns an iter.Seq2 that yields key-value pairs from the map.
100func (m *Map[K, V]) Seq2() iter.Seq2[K, V] {
103 maps.Copy(dst, m.inner)
105 return func(yield func(K, V) bool) {
106 for k, v := range dst {
114// Seq returns an iter.Seq that yields values from the map.
115func (m *Map[K, V]) Seq() iter.Seq[V] {
116 return func(yield func(V) bool) {
117 for _, v := range m.Seq2() {
126 _ json.Unmarshaler = &Map[string, any]{}
127 _ json.Marshaler = &Map[string, any]{}
130func (Map[K, V]) JSONSchemaAlias() any { //nolint
135// UnmarshalJSON implements json.Unmarshaler.
136func (m *Map[K, V]) UnmarshalJSON(data []byte) error {
139 m.inner = make(map[K]V)
140 return json.Unmarshal(data, &m.inner)
143// MarshalJSON implements json.Marshaler.
144func (m *Map[K, V]) MarshalJSON() ([]byte, error) {
147 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