package safemap import "sync" type SafeMap[K comparable, V any] struct { m sync.Map } func NewSafeMap[K comparable, V any]() *SafeMap[K, V] { return &SafeMap[K, V]{ m: sync.Map{}, } } func (sm *SafeMap[K, V]) Store(key K, value V) { sm.m.Store(key, value) } func (sm *SafeMap[K, V]) Load(key K) (value V, ok bool) { val, loaded := sm.m.Load(key) if !loaded { return *new(V), false } return val.(V), true } func (sm *SafeMap[K, V]) Delete(key K) { sm.m.Delete(key) } func (sm *SafeMap[K, V]) Range(f func(K, V) bool) { sm.m.Range(func(key, value any) bool { k, ok1 := key.(K) v, ok2 := value.(V) if !ok1 || !ok2 { return true } return f(k, v) }) } func (sm *SafeMap[K, V]) Len() int { count := 0 sm.Range(func(_ K, _ V) bool { count++ return true }) return count }