Chapter 13

  1. What is context in Go?
    Context is a package that contains a generic interface and some auxiliary functions to return context instances. It is used to synchronize operations between various parts of the application and to carry values.
  2. What's the difference between cancellation, deadline, and timeout?
    There are three different types of expiration for a context—cancellation is an explicit call to a cancellation function by the application, deadline is when the context goes over a specified time, and timeout is when the context survives a specific duration.
  3. What are the best practices when passing values with a context?
    Values passed around with context should be relative to the current scope or request. They should not be used as a way of passing around optional function parameters or variables that are essential to the application. It is also a good idea to use custom private types as keys, because built-in values can be overridden by other packages. Pointers to values are also a solution to such a problem.
  4. Which standard packages already use context?
    There are different packages that use context. The most notable are net/http , which uses context for requests and for server shutdown; net, which uses context for functions such as Dial or Listen; and database/sql, which uses the context as a way to cancel operations such as queries.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.134.76.72