gsync

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

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

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]) Clear

func (sm *Map[K, V]) Clear()

Clear wraps sync.Map.Clear.

func (*Map[K, V]) CompareAndDelete

func (sm *Map[K, V]) CompareAndDelete(key K, old V) bool

CompareAndDelete wraps sync.Map.CompareAndDelete.

💡 NOTE: Newly added in go1.20

func (*Map[K, V]) CompareAndSwap

func (sm *Map[K, V]) CompareAndSwap(key K, old, new V) bool

CompareAndSwap wraps sync.Map.CompareAndSwap.

💡 NOTE: Newly added in go1.20

func (*Map[K, V]) Delete

func (sm *Map[K, V]) Delete(key K)

Delete wraps sync.Map.Delete.

func (*Map[K, V]) Load

func (sm *Map[K, V]) Load(key K) (V, bool)

Load wraps sync.Map.Load.

func (*Map[K, V]) LoadAndDelete

func (sm *Map[K, V]) LoadAndDelete(key K) (V, bool)

LoadAndDelete wraps sync.Map.LoadAndDelete.

func (*Map[K, V]) LoadO

func (sm *Map[K, V]) LoadO(key K) goption.O[V]

LoadO wraps [Load], returns a goption value instead of (V, bool).

func (*Map[K, V]) LoadOrStore

func (sm *Map[K, V]) LoadOrStore(key K, value V) (V, bool)

LoadOrStore wraps sync.Map.LoadOrStore.

func (*Map[K, V]) Range

func (sm *Map[K, V]) Range(f func(K, V) bool)

Range wraps sync.Map.Range.

func (*Map[K, V]) Store

func (sm *Map[K, V]) Store(key K, value V)

Store wraps sync.Map.Store.

func (*Map[K, V]) Swap

func (sm *Map[K, V]) Swap(key K, value V) (V, bool)

Swap wraps sync.Map.Swap.

💡 NOTE: Newly added in go1.20

func (*Map[K, V]) ToMap

func (sm *Map[K, V]) ToMap() map[K]V

ToMap collects all keys and values to a go builtin map.

type Pool

type Pool[T any] struct {
	New func() T
	// contains filtered or unexported fields
}

Pool wraps sync.Pool.

Example
pool := Pool[*int]{
	New: func() *int {
		i := 1
		return &i
	},
}
a := pool.Get()
fmt.Println(*a) // 1
*a = 2
pool.Put(a)
_ = *pool.Get() // possible output: 1 or 2
Output:

1

func (*Pool[T]) Get

func (p *Pool[T]) Get() T

Get wraps sync.Pool.Get.

func (*Pool[T]) Put

func (p *Pool[T]) Put(x T)

Put wraps sync.Pool.Put.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL