-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
68 lines (57 loc) · 1.14 KB
/
main.go
File metadata and controls
68 lines (57 loc) · 1.14 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
"log"
"os"
"github.com/pubgo/dix/v2/dixglobal"
)
type Redis struct {
name string
}
type Handler struct {
Cli *Redis
Cli1 map[string]*Redis
}
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Printf("panic: %v\n", r)
}
}()
dixglobal.Provide(func() *log.Logger {
return log.New(os.Stderr, "example: ", log.LstdFlags|log.Lshortfile)
})
dixglobal.Provide(func(p struct {
L *log.Logger
},
) *Redis {
p.L.Println("init redis")
return &Redis{name: "hello"}
})
dixglobal.Provide(func(l *log.Logger) map[string]*Redis {
l.Println("init redis")
return map[string]*Redis{
"ns": {name: "hello1"},
}
})
dixglobal.Inject(func(r *Redis, l *log.Logger, rr map[string]*Redis) {
l.Println("invoke redis")
fmt.Println("invoke:", r.name)
fmt.Println("invoke:", rr)
})
h := dixglobal.Inject(new(Handler))
if h.Cli.name != "hello" {
panic("inject error")
}
if h.Cli1["ns"].name != "hello1" {
panic("inject error")
}
dixglobal.Inject(func(h Handler) {
if h.Cli.name != "hello" {
panic("inject error")
}
if h.Cli1["ns"].name != "hello1" {
panic("inject error")
}
})
}