github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/base/bobjectstorage/bobjectstorage.go (about)

     1  package bobjectstorage
     2  
     3  import (
     4  	"github.com/songzhibin97/go-baseutils/base/bmap"
     5  	"github.com/songzhibin97/go-baseutils/base/breflect"
     6  )
     7  
     8  var (
     9  	anyMap = bmap.NewSafeAnyBMap[string, any]()
    10  )
    11  
    12  func Set[T any](key string, val T) {
    13  	anyMap.Put(key, val)
    14  }
    15  
    16  func GetSafeAssertion[T any](key string) (T, bool) {
    17  	var zero T
    18  	v, ok := anyMap.Get(key)
    19  	if !ok || breflect.IsNil(v) {
    20  		return zero, false
    21  	}
    22  	nv, ok := v.(T)
    23  	return nv, ok
    24  }
    25  
    26  func Get[T any](key string) T {
    27  	var zero T
    28  	v, ok := anyMap.Get(key)
    29  	if !ok || breflect.IsNil(v) {
    30  		return zero
    31  	}
    32  	return v.(T)
    33  }