Junior — Middle
Milliseid elemente saab praegusesse programmi täitmise konteksti lisada?
sobes.tech AI
Vastus AI-lt
In Go (Golang), the current execution context of a program is typically represented by the context.Context type. It is used to pass information about the execution state, cancellation of operations, and deadlines between goroutines.
The context can include the following elements:
- Cancellation — a signal that an operation should be canceled.
- Deadline — the time after which the operation is considered expired.
- Values — key-value pairs for transmitting additional information (e.g., request identifiers, authentication tokens).
Example of usage:
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
ctx = context.WithValue(ctx, "requestID", "12345")
// Pass ctx to functions that can check for cancellation, deadline, or retrieve values
Thus, the context contains metadata and manages the lifecycle of operations within the program.