Using the context package
Several recipes throughout this book make use of the context
package. This recipe will explore the basics of creating and managing contexts. A good reference for understanding context is https://blog.golang.org/context. Since this blog post was written, context moved from net/context
to a package called context
. This still occasionally causes problems when interacting with third-party libraries such as GRPC.
This recipe will explore setting and getting values for contexts, cancelation, and timeouts.
Getting ready
Refer to the Getting ready section of the Using channels and the select statement recipe in this chapter.
How to do it...
These steps cover writing and running your application:
- From your terminal/console application, create the
chapter9/context
directory and navigate to it. - Copy tests from https://github.com/agtorre/go-cookbook/tree/master/chapter9/context or use this as an exercise to write some of your own code.
- Create a file called
values.go
with the following...