💻 Tech
Contexts can be created from another context, which is why it’s important to know when to cancel a primary context. One way to do this is to manually cancel a timeout. In the example below, an idiomatic way to ensure that the context is cancelled upon exiting the function is demonstrated.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
defer cancel()