Context Package

  • We need a way to propagate request-scoped data down the call graph.
  • We need a way to propagate cancellation signal down the call graph.
  • Across API bundaries to all goroutines involved in handling a request.

Context package serves two primary purpos

  1. Provides API's for cancelling branches of call graph.
  2. Provides a data-bag for transporting request-scoped data through call graph. Image

Context package provides functions to create new context

  • context.Background()
  • context.TODO()
context.Background()
func main(){
    ctx := context.Background()
}
  • Background return an empty context.
  • Root of any context tree.
  • It is never canceled, has no value and has no deadline.
  • Acts as a top level context for incoming request.
context.TODO()
func main(){
    ctx := context.TODO()
}
  • TODO() return an empty context.
  • TODO's intended purpose is to serve as a placeholder.
© Kimi Tsai all right reserved.            Updated : 2023-07-12 09:04:54

results matching ""

    No results matching ""

    results matching ""

      No results matching ""