1 | import context |
2 | |
3 | const not_found_value = &Value{ |
4 | val: 'key not found' |
5 | } |
6 | |
7 | struct Value { |
8 | val string |
9 | } |
10 | |
11 | // This example demonstrates how a value can be passed to the context |
12 | // and also how to retrieve it if it exists. |
13 | fn test_with_value() { |
14 | f := fn (ctx context.Context, key context.Key) &Value { |
15 | if value := ctx.value(key) { |
16 | match value { |
17 | Value { |
18 | return value |
19 | } |
20 | else {} |
21 | } |
22 | } |
23 | return not_found_value |
24 | } |
25 | |
26 | key := 'language' |
27 | value := &Value{ |
28 | val: 'VAL' |
29 | } |
30 | ctx := context.with_value(context.background(), key, value) |
31 | |
32 | assert value == f(ctx, key) |
33 | assert not_found_value == f(ctx, 'color') |
34 | } |