gitee.com/quant1x/gox@v1.21.2/concurrent/hashmap.go (about)

     1  package concurrent
     2  
     3  import "sync"
     4  
     5  type v1HashMap[K comparable, V any] struct {
     6  	mutex sync.RWMutex
     7  	m     map[K]V
     8  	once  sync.Once
     9  }
    10  
    11  func v1NewHashmap[K comparable, V any]() *v1HashMap[K, V] {
    12  	hashmap := v1HashMap[K, V]{}
    13  	hashmap.m = make(map[K]V)
    14  	return &hashmap
    15  }
    16  
    17  func (this *v1HashMap[K, V]) Get(key K) (V, bool) {
    18  	this.mutex.RLock()
    19  	v, ok := this.m[key]
    20  	this.mutex.RUnlock()
    21  	return v, ok
    22  }
    23  
    24  func (this *v1HashMap[K, V]) Put(key K, value V) {
    25  	this.mutex.Lock()
    26  	this.m[key] = value
    27  	this.mutex.Unlock()
    28  }