npx skills add https://github.com/cxuu/golang-skills --skill go-context使用 Context 的函数应将其作为第一个参数接收:
func F(ctx context.Context, /* other arguments */) error
func ProcessRequest(ctx context.Context, req *Request) (*Response, error)
这是 Go 语言中一个强有力的约定,它使得 Context 的传递在代码库中清晰可见且保持一致。
不要向结构体类型添加 Context 成员。相反,将 ctx 作为参数传递给每个需要它的方法:
// 错误做法:Context 存储在结构体中
type Worker struct {
ctx context.Context // 不要这样做
}
// 正确做法:Context 传递给方法
type Worker struct{ /* ... */ }
func (w *Worker) Process(ctx context.Context) error {
// Context 显式传递 — 生命周期清晰
}
例外情况:那些签名必须与标准库或第三方库中的接口匹配的方法可能需要变通处理。
不要创建自定义的 Context 类型,也不要在函数签名中使用 context.Context 以外的接口:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
// 错误做法:自定义 Context 类型
type MyContext interface {
context.Context
GetUserID() string
}
// 正确做法:使用标准的 context.Context 并提取值
func Process(ctx context.Context) error {
userID := GetUserID(ctx)
}
按优先顺序考虑以下选项:
Context 值适用于:
Context 值不适用于:
在派生 Context(WithTimeout、WithCancel、WithDeadline)、在循环或 HTTP 处理程序中检查取消、使用带类型键的 Context 值,或需要快速参考表时,请阅读 references/PATTERNS.md。
在创建派生的 Context 后,应立即使用 defer cancel():
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
select {
case <-ctx.Done():
return ctx.Err()
default:
// 执行工作
}
Context 是不可变的 — 将同一个 ctx 传递给多个共享相同截止时间和取消信号的并发调用是安全的。
ctx.Err() 取消错误时,请参阅 go-error-handling每周安装量
242
代码仓库
GitHub 星标数
56
首次出现
2026年1月27日
安全审计
安装于
github-copilot226
opencode215
gemini-cli214
codex214
cursor211
kimi-cli210
Functions that use a Context should accept it as their first parameter :
func F(ctx context.Context, /* other arguments */) error
func ProcessRequest(ctx context.Context, req *Request) (*Response, error)
This is a strong convention in Go that makes context flow visible and consistent across codebases.
Do not add a Context member to a struct type. Instead, pass ctx as a parameter to each method that needs it:
// Bad: Context stored in struct
type Worker struct {
ctx context.Context // Don't do this
}
// Good: Context passed to methods
type Worker struct{ /* ... */ }
func (w *Worker) Process(ctx context.Context) error {
// Context explicitly passed — lifetime clear
}
Exception : Methods whose signature must match an interface in the standard library or a third-party library may need to work around this.
Do not create custom Context types or use interfaces other than context.Context in function signatures:
// Bad: Custom context type
type MyContext interface {
context.Context
GetUserID() string
}
// Good: Use standard context.Context with value extraction
func Process(ctx context.Context) error {
userID := GetUserID(ctx)
}
Consider these options in order of preference:
Context values are appropriate for:
Context values are not appropriate for:
Read references/PATTERNS.md when deriving contexts (WithTimeout, WithCancel, WithDeadline), checking cancellation in loops or HTTP handlers, using context values with typed keys, or needing the quick reference table.
Always defer cancel() immediately after creating a derived context:
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
select {
case <-ctx.Done():
return ctx.Err()
default:
// Do work
}
Contexts are immutable — it's safe to pass the same ctx to multiple concurrent calls that share the same deadline and cancellation signal.
ctx.Err() cancellation errorsWeekly Installs
242
Repository
GitHub Stars
56
First Seen
Jan 27, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
github-copilot226
opencode215
gemini-cli214
codex214
cursor211
kimi-cli210
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
106,200 周安装