Passing context to other functions

tags: learning go programming

content

query := '....'
ctx := context.Background()
ctx = context.WithTimeout(ctx, 5 * time.Seconds)
res, err := google.Search(ctx, query)

in the above code

  • a context with Timeout is created
  • the context is passed to other function google.Search
  • so that the other function will be canceled automatically when the timeout is reached

up

down

reference