Finally the Context interface clicked for me. I was reading the Network Programming with Go book and the example of the UDP server uses the context package to handle a server life-cycle across goroutines.
Consider the example
echoServer function. It is used to create a UDP echo server
replying to any incoming packet with the same packet.
The author does not intend to keep the server
s around but he still has to manage
its life-cycle, e.g. eventually calling the
Close() method. The function returns
the local address of the server because that is all the author needed but there is
no way to close the server later on without returning the variable
s to the caller.
This problem warrant the use of messaging through channels. Channels are a data type
that is used to sync two goroutines. We can pass a channel
ch we control to the
function and have a goroutine wait for a message on it to close the underlying server
Now the caller is in charge of closing the underlying server and the resource will not leak.
The context package provides a way to reuse this pattern using an interface instead of a channel.
A context interface can be used to propagate a cancellation message
to the server because it wraps a channel. To access the channel of the context
we user the
The selling point of the
Context type is that it is an interface. Many types
can be used in place of a
Context argument. This means that we can have different
behavior as long as we satisfy the interface.
The most common context types are the one provided by the context package.
And the rest are derived using the
Finally we can see a full example using the
Context interface to propagate cancellation from
a goroutine to another.