forked from baidu/brcc-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_client_test.go
65 lines (53 loc) · 1.14 KB
/
example_client_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
56
57
58
59
60
61
62
63
64
65
/*
@Time : 2021-06-11
@Author : xiemalin
*/
package rcc
import "fmt"
// ExampleNewConf the example to show use client to connect server
func ExampleClient() {
client, err := NewClientWithConfFile(TestConfFile)
if err != nil {
return
}
err = client.Start()
if err != nil {
return
}
defer client.Stop()
keys := client.GetAllKeys()
for _, v := range keys {
client.GetValue(v, "DefaultValue")
}
s := struct {
Key string `rcc:"mykey"`
}{}
client.Bind(&s)
}
// ExampleClient_Watch he example to show use Watch function
func ExampleClient_Watch() {
client, err := NewClientWithConfFile(TestConfFile)
if err != nil {
return
}
err = client.Start()
if err != nil {
return
}
defer client.Stop()
// define update callback for Watch function
callback := func(ce *ChangeEvent) {
// 建议defer捕获协程panic
defer func() {
if r := recover(); r != nil {
fmt.Println("watch update callback panic")
}
}()
for key, change := range ce.Changes {
if change.ChangeType == ADD || change.ChangeType == MODIFY {
fmt.Println("changed item", key, change.NewValue, change.OldValue)
}
}
}
client.Watch(callback)
}