internal/component: Move Pool into lazy package
This commit is contained in:
parent
bcfd0765b0
commit
7c3c84e116
8 changed files with 261 additions and 139 deletions
31
pkg/lazy/lazy_test.go
Normal file
31
pkg/lazy/lazy_test.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package lazy
|
||||
|
||||
import "fmt"
|
||||
|
||||
func ExampleLazy() {
|
||||
|
||||
var lazy Lazy[int]
|
||||
|
||||
// the first invocation to lazy will be called and set the value
|
||||
fmt.Println(lazy.Get(func() int { return 42 }))
|
||||
|
||||
// the second invocation will not call init again, using the first value
|
||||
fmt.Println(lazy.Get(func() int { return 43 }))
|
||||
|
||||
// Set can be used to set a specific value
|
||||
lazy.Set(0)
|
||||
fmt.Println(lazy.Get(func() int { panic("never called") }))
|
||||
|
||||
// Output: 42
|
||||
// 42
|
||||
// 0
|
||||
}
|
||||
|
||||
func ExampleLazy_nil() {
|
||||
var lazy Lazy[int]
|
||||
|
||||
// passing nil as the initialization function causes the zero value to be set
|
||||
fmt.Println(lazy.Get(nil))
|
||||
|
||||
// Output: 0
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue