-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
43 lines (34 loc) · 880 Bytes
/
example_test.go
File metadata and controls
43 lines (34 loc) · 880 Bytes
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
package contextx_test
import (
"context"
"fmt"
"github.com/rakunlabs/contextx"
)
func Example() {
ctx := contextx.WithValue(context.Background(), "secret", "xxx")
if v, ok := contextx.Value[string](ctx, "secret"); ok {
fmt.Println("[" + v + "]")
}
ctx = contextx.WithValue(ctx, "another", "yyy", contextx.WithKey("customKey"))
if v, ok := contextx.Value[string](ctx, "another"); ok {
fmt.Println("[" + v + "]")
} else {
// expected not found
fmt.Println("not found")
}
if v, ok := contextx.Value[string](ctx, "secret", contextx.WithKey("customKey")); ok {
fmt.Println("[" + v + "]")
} else {
// expected not found
fmt.Println("not found")
}
if v, ok := contextx.Value[string](ctx, "another", contextx.WithKey("customKey")); ok {
// expected found
fmt.Println("[" + v + "]")
}
// Output:
// [xxx]
// not found
// not found
// [yyy]
}