mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
context: add a new context module, based on Golang's context, intended to be used in webservers (#9563)
This commit is contained in:
committed by
GitHub
parent
b54188dfea
commit
07a6f4e445
23
vlib/context/value_test.v
Normal file
23
vlib/context/value_test.v
Normal file
@@ -0,0 +1,23 @@
|
||||
import context
|
||||
|
||||
type ValueContextKey = string
|
||||
|
||||
// This example demonstrates how a value can be passed to the context
|
||||
// and also how to retrieve it if it exists.
|
||||
fn test_with_value() {
|
||||
f := fn (ctx context.ValueContext, key ValueContextKey) string {
|
||||
if value := ctx.value(key) {
|
||||
if !isnil(value) {
|
||||
return *(&string(value))
|
||||
}
|
||||
}
|
||||
return 'key not found'
|
||||
}
|
||||
|
||||
key := ValueContextKey('language')
|
||||
value := 'VAL'
|
||||
ctx := context.with_value(context.background(), key, &value)
|
||||
|
||||
assert value == f(ctx, key)
|
||||
assert 'key not found' == f(ctx, ValueContextKey('color'))
|
||||
}
|
||||
Reference in New Issue
Block a user