Sobes.tech
Junior — Middle

Millist teavet võib rakenduses konteksti kaudu edastada?

sobes.tech AI

Vastus AI-lt

In Go, information related to managing the lifecycle of a request or operation is usually passed through the context (context.Context`), for example:

  • Cancelling an operation (via the Done() channel).
  • Deadlines and timeouts (via Deadline()).
  • Values needed throughout the call chain, such as request identifiers, authentication tokens, metadata.

Important: do not place large objects, states, or data unrelated to request management into the context, to avoid violating its purpose and prevent memory leaks.

Example of passing a value through the context:

ctx := context.WithValue(context.Background(), "requestID", "12345")
requestID := ctx.Value("requestID")