what’s context.Background()
tags: learning go programming
content
Backgroundis the root of all contextscontext.Background()returns an empty context
WithCancel and WithTimeout
- these two functions copies parent context (input arg)
- returns a new context and a cancel function
WithTimeouttakes one more arg (timeout duration)- when timeout duration is reached, context is auto canceled
ctx, cancelFunc = context.WithCancel(ctx)
ctx, cancelFunc = context.WithTimeout(ctx, 10 * time.Seconds)- a
contexthas aDonechannel- canceling the context is to close the
Donechannel - calling
cancelFunccloses theDonechannel
- canceling the context is to close the