July 2019
Intermediate to advanced
458 pages
12h 12m
English
Background is an empty context that doesn't get cancelled, hasn't got a deadline, and doesn't hold any values. It is mostly used by the main function as the root context or for testing purposes. The following is some example code for this context:
func main() { ctx := context.Background() done := ctx.Done() for i :=0; ;i++{ select { case <-done: return case <-time.After(time.Second): fmt.Println("tick", i) } }}
The full example is available here: https://play.golang.org/p/y_3ip7sdPnx.
We can see that, in the context of the example, the loop goes on infinitely because the context is never completed.
Read now
Unlock full access