https://github.com/nhatthm/go-once
A simple library to do something exactly once
https://github.com/nhatthm/go-once
go golang once
Last synced: 6 months ago
JSON representation
A simple library to do something exactly once
- Host: GitHub
- URL: https://github.com/nhatthm/go-once
- Owner: nhatthm
- License: mit
- Created: 2024-01-13T09:42:24.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-01-06T16:24:36.000Z (9 months ago)
- Last Synced: 2025-02-10T09:45:02.744Z (8 months ago)
- Topics: go, golang, once
- Language: Go
- Homepage:
- Size: 42 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Once
A simple library to do something exactly once.
[](https://github.com/nhatthm/go-once/releases/latest)
[](https://github.com/nhatthm/go-once/actions/workflows/test.yaml)
[](https://codecov.io/gh/nhatthm/go-once)
[](https://goreportcard.com/report/go.nhat.io/once)
[](https://pkg.go.dev/go.nhat.io/once)
[](https://www.paypal.com/donate/?hosted_button_id=PJZSGJN57TDJY)## Prerequisites
- `Go >= 1.21`
## Install
```bash
go get go.nhat.io/once
```## Types
| Type | Description |
|----------------|---------------------------------------------------------------------|
| `Once` | An alias of [`sync.Once`](https://pkg.go.dev/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. |## Examples
```go
package once_testimport (
"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
}```
## Donation
If this project help you reduce time to develop, you can give me a cup of coffee :)
### Paypal donation
[](https://www.paypal.com/donate/?hosted_button_id=PJZSGJN57TDJY)
or scan this
![]()