command
Version:
v0.1.18
Opens a new window with list of versions in this module.
Published: Jul 1, 2026
License: MIT
Opens a new window with license information.
Imports: 3
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
README
¶
contextx basic example
最小可运行示例,展示 contextx 的核心用法。
运行
go run ./examples/contextx-basic
预期输出
request_id: req_abc
trace_id: trace_xyz
subject_id: u_123
tenant: t_acme
is_admin: true
can_write: true
for errorx: request_id=req_abc trace_id=trace_xyz
anonymous subject_id:
feature X enabled
merged trace_id: trace_parent
merged request_id: req_job
这个示例展示了什么
- InjectRequestContext:handler 边界一次性注入所有请求字段
- 提取值:request_id / trace_id / principal / tenant / logger
- Principal 方法:HasRole / HasScope / IsAuthenticated
- RequestIDAndTraceIDFromContext:一次取两个 ID(给 errorx 用)
- nil 安全:匿名请求的 SubjectIDFromContext 返回 ""
- 自定义值:With / From 用于特性开关等
- Merge:合并两个 context,继承两者的值
相关文档
contextx/README.md — 完整用户指南
docs/ai/contextx.md — AI 编码食谱(10 场景)
contextx/example_test.go — 所有 API 的 Go 标准 Example
contextx/example_business_test.go — 10 个完整业务场景
Documentation
¶
Package main demonstrates contextx basic usage.
Run:
go run ./examples/contextx-basic
Source Files
¶
Click to show internal directories.
Click to hide internal directories.