A simple library to do something exactly once.
Go >= 1.21
go get go.nhat.io/once
Type | Description |
---|---|
Once |
An alias of sync.Once . |
Func |
A function that can be executed exactly once. |
Value |
A function that returns value can be executed exactly once. |
Values |
A function that returns values can be executed exactly once. |
FuncMap |
A map of functions that can be executed exactly once. |
ValueMap |
A map of functions that return value can be executed exactly once. |
ValuesMap |
A map of functions that return values can be executed exactly once. |
LazyValueMap |
A map contains values that are initialized on the first access. |
package once_test
import (
"fmt"
"go.nhat.io/once"
)
func ExampleLazyValueMap() {
type Person struct {
ID string
Name string
}
people := once.LazyValueMap[string, *Person]{
New: func(key string) *Person {
return &Person{ID: key}
},
}
instance1 := people.Get("1")
instance2 := people.Get("1")
fmt.Println(instance2.Name)
instance1.Name = "John Doe"
fmt.Println(instance2.Name)
// Output:
//
// John Doe
}
If this project help you reduce time to develop, you can give me a cup of coffee :)
or scan this