SetVar sets a value in the context's variable table with the given key. It overwrites any previous value with the same key. If the value is nil (note: non-nil interface with nil underlying value does not count) and the key exists in the table, the key+value will be deleted from the table.
(ctx context.Context, key string, value any)
| 446 | // underlying value does not count) and the key exists in |
| 447 | // the table, the key+value will be deleted from the table. |
| 448 | func SetVar(ctx context.Context, key string, value any) { |
| 449 | varMap, ok := ctx.Value(VarsCtxKey).(map[string]any) |
| 450 | if !ok { |
| 451 | return |
| 452 | } |
| 453 | if value == nil { |
| 454 | if _, ok := varMap[key]; ok { |
| 455 | delete(varMap, key) |
| 456 | return |
| 457 | } |
| 458 | } |
| 459 | varMap[key] = value |
| 460 | } |
| 461 | |
| 462 | // Interface guards |
| 463 | var ( |