28 lines
391 B
Go
28 lines
391 B
Go
|
package types
|
||
|
|
||
|
import (
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
type Map[K comparable, V any] struct {
|
||
|
rw sync.RWMutex
|
||
|
m map[K]V
|
||
|
}
|
||
|
|
||
|
func NewMap[K comparable, V any]() *Map[K, V] {
|
||
|
return &Map[K, V]{m: make(map[K]V)}
|
||
|
}
|
||
|
|
||
|
func (t *Map[K, V]) Set(k K, v V) {
|
||
|
defer t.rw.Unlock()
|
||
|
t.rw.Lock()
|
||
|
t.m[k] = v
|
||
|
}
|
||
|
|
||
|
func (t *Map[K, V]) Get(k K) (val V, ok bool) {
|
||
|
defer t.rw.RUnlock()
|
||
|
t.rw.RLock()
|
||
|
val, ok = t.m[k]
|
||
|
return
|
||
|
}
|