https://github.com/hymkor/go-lazy
lazy initialization for golang
https://github.com/hymkor/go-lazy
Last synced: 18 days ago
JSON representation
lazy initialization for golang
- Host: GitHub
- URL: https://github.com/hymkor/go-lazy
- Owner: hymkor
- License: mit
- Created: 2020-09-24T13:28:09.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-03-01T09:54:33.000Z (about 1 year ago)
- Last Synced: 2025-04-10T22:53:40.783Z (18 days ago)
- Language: Go
- Homepage: https://pkg.go.dev/github.com/hymkor/go-lazy
- Size: 18.6 KB
- Stars: 10
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
lazy
====[](https://travis-ci.com/github/hymkor/go-lazy)
[](https://pkg.go.dev/github.com/hymkor/go-lazy)Provides support for lazy initialization by generics in Go1.18
example 1
---------```example1.go
package mainimport (
"github.com/hymkor/go-lazy"
)var s1 = lazy.New(func() string {
println("s1 initialize")
return "Foo"
})func main() {
println("start")
println(s1.Value())
println(s1.Value())
}
``````go run example1.go|
start
s1 initialize
Foo
Foo
```example 2
---------Same as example 1. Light but long
```example2.go
package mainimport (
"github.com/hymkor/go-lazy"
)var s1 = lazy.Of[string]{
New: func() string {
println("s1 initialize")
return "Foo"
},
}func main() {
println("start")
println(s1.Value())
println(s1.Value())
}
```example 3
---------Two values version like `"sync".OnceValues`
```example3.go
package mainimport (
"github.com/hymkor/go-lazy"
)var counter = 0
var s1 = lazy.Two[string, int]{
New: func() (string, int) {
println("s1 initialize")
counter++
return "Foo", counter
},
}func main() {
println("start")
println(s1.Values())
println(s1.Values())
}
``````go run example3.go|
start
s1 initialize
Foo 1
Foo 1
```