Provides support for lazy initialization by generics in Go1.18
package main
import (
"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())
}
start
s1 initialize
Foo
Foo
Same as example 1. Light but long
package main
import (
"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())
}
Two values version like "sync".OnceValues
package main
import (
"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())
}
start
s1 initialize
Foo 1
Foo 1