go-context

cxuu/golang-skills · updated Apr 8, 2026

$npx skills add https://github.com/cxuu/golang-skills --skill go-context
0 commentsdiscussion
summary

Functions that use a Context should accept it as their first parameter:

skill.md

Go Context Usage

Context as First Parameter

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.


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:

// 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.


Don't Create Custom Context Types

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)
}

Where to Put Application Data

Consider these options in order of preference:

  1. Function parameters — most explicit and type-safe
  2. Receiver — for data that belongs to the type
  3. Globals — for truly global configuration (use sparingly)
  4. Context value — only for request-scoped data

Context values are appropriate for:

  • Request IDs and trace IDs
  • Authentication/authorization info that flows with requests
  • Deadlines and cancellation signals

Context values are not appropriate for:

  • Optional function parameters
  • Data that could be passed explicitly
  • Configuration that doesn't vary per-request

Common Patterns

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.

Deriving Contexts

Always defer cancel() immediately after creating a derived context:

ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

Checking Cancellation

select {
case <-ctx.Done():
    return ctx.Err()
default:
    // Do work
}

Context Immutability

Contexts are immutable — it's safe to pass the same ctx to multiple concurrent calls that share the same deadline and cancellation signal.


Related Skills

  • Goroutine coordination: See go-concurrency when using context for goroutine cancellation, select-based timeouts, or errgroup
  • Error handling: See go-error-handling when deciding how to wrap or return ctx.Err() cancellation errors
  • Interface design: See go-interfaces when designing APIs that accept context alongside interfaces
  • Request-scoped logging: See go-logging when injecting loggers into context or adding request IDs to structured log output

Discussion

Product Hunt–style comments (not star reviews)
  • No comments yet — start the thread.
general reviews

Ratings

4.828 reviews
  • Pratham Ware· Dec 24, 2024

    go-context has been reliable in day-to-day use. Documentation quality is above average for community skills.

  • Yash Thakker· Nov 15, 2024

    Solid pick for teams standardizing on skills: go-context is focused, and the summary matches what you get after install.

  • Dhruvi Jain· Oct 6, 2024

    We added go-context from the explainx registry; install was straightforward and the SKILL.md answered most questions upfront.

  • Emma Desai· Sep 25, 2024

    Solid pick for teams standardizing on skills: go-context is focused, and the summary matches what you get after install.

  • Piyush G· Sep 17, 2024

    Useful defaults in go-context — fewer surprises than typical one-off scripts, and it plays nicely with `npx skills` flows.

  • Evelyn Abbas· Sep 13, 2024

    I recommend go-context for anyone iterating fast on agent tooling; clear intent and a small, reviewable surface area.

  • Emma Tandon· Aug 16, 2024

    We added go-context from the explainx registry; install was straightforward and the SKILL.md answered most questions upfront.

  • Shikha Mishra· Aug 8, 2024

    go-context is among the better-maintained entries we tried; worth keeping pinned for repeat workflows.

  • Evelyn Ramirez· Aug 4, 2024

    go-context reduced setup friction for our internal harness; good balance of opinion and flexibility.

  • Fatima Liu· Jul 23, 2024

    Registry listing for go-context matched our evaluation — installs cleanly and behaves as described in the markdown.

showing 1-10 of 28

1 / 3