Documentation
¶
Overview ¶
Package gsync provides generics wrappers of sync package.
Currently, we provide these wrappers: - Map - Pool - OnceFunc, OnceValue, OnceValues
Example ¶
onceFunc := OnceFunc(func() { fmt.Println("OnceFunc") })
onceFunc() // "OnceFunc"
onceFunc() // (no output)
onceFunc() // (no output)
i := 1
onceValue := OnceValue(func() int { i++; return i })
fmt.Println(onceValue()) // 2
fmt.Println(onceValue()) // 2
onceValues := OnceValues(func() (int, error) { i++; return i, nil })
fmt.Println(onceValues()) // 3 nil
fmt.Println(onceValues()) // 3 nil
Output: OnceFunc 2 2 3 <nil> 3 <nil>
Index ¶
- func OnceFunc(f func()) func()
- func OnceValue[T any](f func() T) func() T
- func OnceValues[T1, T2 any](f func() (T1, T2)) func() (T1, T2)
- type Map
- func (sm *Map[K, V]) Clear()
- func (sm *Map[K, V]) CompareAndDelete(key K, old V) bool
- func (sm *Map[K, V]) CompareAndSwap(key K, old, new V) bool
- func (sm *Map[K, V]) Delete(key K)
- func (sm *Map[K, V]) Load(key K) (V, bool)
- func (sm *Map[K, V]) LoadAndDelete(key K) (V, bool)
- func (sm *Map[K, V]) LoadO(key K) goption.O[V]
- func (sm *Map[K, V]) LoadOrStore(key K, value V) (V, bool)
- func (sm *Map[K, V]) Range(f func(K, V) bool)
- func (sm *Map[K, V]) Store(key K, value V)
- func (sm *Map[K, V]) Swap(key K, value V) (V, bool)
- func (sm *Map[K, V]) ToMap() map[K]V
- type Pool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func OnceFunc ¶
func OnceFunc(f func()) func()
OnceFunc returns a function that invokes f only once when returned function is firstly called.
func OnceValue ¶
func OnceValue[T any](f func() T) func() T
OnceValue returns a function as value getter. Value is returned by function f, and f is invoked only once when returned function is firstly called.
This function can be used to lazily initialize a value, as replacement of the packages-level init function. For example:
var DB *sql.DB
func init() {
// 💡 NOTE: DB is initialized here.
DB, _ = sql.Open("mysql", "user:password@/dbname")
}
func main() {
DB.Query(...)
}
Can be rewritten to:
var DB = OnceValue(func () *sql.DB {
return gresult.Of(sql.Open("mysql", "user:password@/dbname")).Value()
})
func main() {
// 💡 NOTE: DB is *LAZILY* initialized here.
DB().Query(...)
}
💡 HINT:
func OnceValues ¶
func OnceValues[T1, T2 any](f func() (T1, T2)) func() (T1, T2)
OnceValues returns a function as values getter. Values are returned by function f, and f is invoked only once when returned function is firstly called.
Types ¶
type Map ¶
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Map wraps sync.Map.
Example ¶
sm := Map[string, int]{}
sm.Store("k", 1)
fmt.Println(sm.Load("k")) // 1 true
fmt.Println(sm.LoadO("k").Value()) // 1
sm.Store("k", 2)
fmt.Println(sm.Load("k")) // 2 true
fmt.Println(sm.LoadAndDelete("k")) // 2 true
fmt.Println(sm.Load("k")) // 0 false
fmt.Println(sm.LoadOrStore("k", 3)) // 3 false
fmt.Println(sm.Load("k")) // 3 true
fmt.Println(sm.ToMap()) // {"k":3}
Output: 1 true 1 2 true 2 true 0 false 3 false 3 true map[k:3]
func (*Map[K, V]) CompareAndDelete ¶
CompareAndDelete wraps sync.Map.CompareAndDelete.
💡 NOTE: Newly added in go1.20
func (*Map[K, V]) CompareAndSwap ¶
CompareAndSwap wraps sync.Map.CompareAndSwap.
💡 NOTE: Newly added in go1.20
func (*Map[K, V]) LoadAndDelete ¶
LoadAndDelete wraps sync.Map.LoadAndDelete.
func (*Map[K, V]) LoadOrStore ¶
LoadOrStore wraps sync.Map.LoadOrStore.