From the course: Go for Developers: Practical Techniques for Effective Coding
Unlock this course with a free trial
Join today to access over 24,900 courses taught by industry experts.
Contexts - Go Tutorial
From the course: Go for Developers: Practical Techniques for Effective Coding
Contexts
- Introduced in Go 1.7, the Context package was introduced to provide a cleaner way than the use of channels for managing cancellation and timeouts across Go routines. While the scope and API footprint of the package is pretty small, it was a welcome addition to the language when it was introduced. The Context package defines the context.Context type, which carries deadlines, cancellation signal, and other request-scoped values across API boundaries in between processes. Context is mostly used for controlling concurrent systems in your application. The Context interface consists of four methods. These methods provide us the ability to listen for cancellation and timeout events, retrieve values from the Context hierarchy, and finally a way to check what error, if any, caused the Context to be canceled. The Context interface implements several of the channel patterns we have already seen, such as having a Done channel that can be listened to for cancellation. We will cover each of these…