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