March 2019
Intermediate to advanced
336 pages
9h 9m
English
Contexts can be implemented in functions executed in Go routines. Contexts are used instead of channel in the code for passing information between processes. The following code snippet shows the usage of context:
//main package has examples shown// in Go Data Structures and algorithms bookpackage main// importing errors,context,log and time packagesimport ( "errors" "golang.org/x/net/context" "log" "time")// main methodfunc main() { var delay time.Duration delay = time.Millisecond var cancel context.CancelFunc var contex context.Context contex, cancel = context.WithTimeout(context.Background(), delay) go func(context.Context) { <-contex.Done() log.Printf("contex done") }(contex) _ = cancel time.Sleep(delay ...Read now
Unlock full access