This is a creation in Article, where the information may have evolved or changed.
What is context
Starting from go1.7, the Golang.org/x/net/context package entered the standard library formally as a context package. So, what exactly does this package do? According to the official document description:
Package context defines the context type, which carries deadlines, cancelation signals, and other request-scoped values AC Ross API boundaries and between processes.
In other words, through the context, we can easily goroutine the same request generated by the constraints of management, you can set the timeout, deadline, or even cancel all the Goroutine related to the request. Figuratively say, if a request come over, need a to do things, and a let B to do some things, B let c to do some things, a, B, C is three related goroutine, then the question came: If in a, B, C is still processing things when the request was canceled, So how to gracefully close Goroutine A, B, C at the same time? It's time for the context pack to play.
How to use the context
Before you begin to explain how to use the context, let's look at what the context has to do with the method definition.
Let's take a look at the code example of the context:
package mainimport ( "context" "log" "net/http" _ "net/http/pprof" "time")func main() { go http.ListenAndServe(":8989", nil) ctx, cancel := context.WithCancel(context.Background()) go func() { time.Sleep(3 * time.Second) cancel() }() log.Println(Add(ctx)) select {}}func C(ctx context.Context) string { select { case <-ctx.Done(): return "C Done" } return ""}func B(ctx context.Context) string { ctx, _ = context.WithCancel(ctx) go log.Println(C(ctx)) select { case <-ctx.Done(): return "B Done" } return ""}func A(ctx context.Context) string { go log.Println(B(ctx)) select { case <-ctx.Done(): return "A Done" } return ""}
Operation Result:
2016/12/25 22:27:00 A Done
2016/12/25 22:27:00 C Done
2016/12/25 22:27:00 B Done
Pprof, at first:
Just started. png
After the end of A, B and C:
After end. png
Here, we use the HTTP pprof to see how many goroutine are executing at runtime,
go http.ListenAndServe(":8989", nil)
This is the start of an HTTP server to view some of the program's running information.
We use context.Background() it to instantiate a context, then call WithCancel() the method to return a context and a cancellation method, and call the Cancel method after 3 seconds to close Goroutine A, B, C. From the running results of the program, we call a Cancel method, and the sub-goroutine ctx.Done() receive a close signal. From the pprof can also be seen, from the beginning there are 5 goroutine, to close the remaining two, that is, a, B, C three goroutine have been closed.
The example here is called directly context.WithCancel() , and we can also use context.WithTimeout() and context.WithDeadline() to set the Goroutine time-out and the final run time. Specific usage can look at the official documents, here is not elaborate. There is another method that is not used in the example, that is context.WithValue() . This method is used to pass the shared variables of the related goroutine in this request processing, which is different from the global variable, because it is only valid within this request scope.
Usage Specification for context
In the latest 1.8 beta release, a number of related packages have been added to the context, such as the database package. So what do you need to be aware of when using the context?
- Instead of storing the context in a struct, explicitly pass it
- The context is used as the first parameter, and the variable is generally named CTX
- Even if the program allows, do not pass in a nil context, if you do not know whether to use the context,
context.TODO() instead of
context.WithValue()is used only to pass the value of the request scope, do not use it to pass optional parameters
- Even if it is used by many different goroutine, the context is also safe.
Context of the preliminary understanding of the first time to say so much, in the future can study the source to see how the context is implemented.
Merry Christmas!
Reference article:
Use of the context of the Golang
Golang context