← ClaudeAtlas

go-contextlisted

Go context.Context usage patterns including parameter placement, avoiding struct embedding, and proper propagation. Use when working with context.Context in Go code for cancellation, deadlines, and request-scoped values.
dwana1/golang-skills · ★ 0 · AI & Automation · score 72
Install: claude install-skill dwana1/golang-skills
# Go Context Usage `context.Context` carries security credentials, tracing information, deadlines, and cancellation signals across API and process boundaries. Go programs pass contexts explicitly along the entire function call chain. > Based on [Go Wiki CodeReviewComments - Contexts](https://github.com/golang/wiki/blob/master/CodeReviewComments.md#contexts). --- ## Context as First Parameter Functions that use a Context should accept it as their **first parameter**: ```go // Good: Context is 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. --- ## Don't Store Context in Structs Do not add a Context member to a struct type. Instead, pass `ctx` as a parameter to each method that needs it: ```go // Bad: Context stored in struct type Worker struct { ctx context.Context // Don't do this // ... } func (w *Worker) Process() error { // Uses w.ctx - context lifetime unclear } ``` ```go // 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. --- ## Don't Create Custom C