Junior — Middle
What arguments does the Context.WithTimeout method expect in Go?
sobes.tech AI
Answer from AI
The context.WithTimeout method in Go takes two arguments:
- A parent context (
context.Context) from which a new context with a timeout will be derived. - A timeout duration (
timeout time.Duration) that specifies the maximum lifetime of the new context.
The function returns the new context and a cancel function (cancelFunc), which should be called to release resources.
Example usage:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Use ctx in operations that should complete within 5 seconds