-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_memoize_test.go
55 lines (51 loc) · 1.1 KB
/
example_memoize_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package nject_test
import (
"fmt"
"github.com/muir/nject"
)
// Memoize implies Chacheable. To make sure that Memoize can actually function
// as desired, also mark functions with MustCache.
// With the same inputs, cached answers
// are always used. The cache lookup examines the values passed, but does not
// do a deep inspection.
func ExampleMemoize() {
type aStruct struct {
ValueInStruct int
}
structProvider := nject.Memoize(func(ip *int, i int) *aStruct {
return &aStruct{
ValueInStruct: i * *ip,
}
})
exampleInt := 7
ip := &exampleInt
_ = nject.Run("chain1",
2,
ip,
structProvider,
func(s *aStruct) {
fmt.Println("first input", s.ValueInStruct, "value set to 22")
s.ValueInStruct = 22
},
)
_ = nject.Run("chain2",
3,
ip,
structProvider,
func(s *aStruct) {
fmt.Println("different inputs", s.ValueInStruct)
},
)
exampleInt = 33
_ = nject.Run("chain3",
2,
ip,
structProvider,
func(s *aStruct) {
fmt.Println("same object as first", s.ValueInStruct)
},
)
// Output: first input 14 value set to 22
// different inputs 21
// same object as first 22
}