This can be a dumb question due to I am new to context, my apologies first if I should not ask here.
For each operation with the client, it needs a context object. Should I create and save the only one context as the global variable then use it, or create it by context.Background() every time when it is required then pass it to the function, or by other methods?
Thanks!
Hey @lynkas! That's not a dumb question at all, if you're new to Go's context, I'll recommend you to read more about it here.
Sorry in advance that I'm keeping it short, I don't have too much time at the moment, and I prefer to "triage" issues as fast as I can. Also, some experts may explain it better than me.
There are many examples where Go context can be useful, here are some of our use cases:
gqlgen for supporting field collections)And there are more - like I mentioned above, I'll recommended you to read about it before.
Should I create and save the only one context as the global variable then use it, or create it by context.Background() every time when it is required then pass it to the function, or by other methods?
For testing, you can context.Background(), for real code, I'll recommend you to use http.Reuqest.Context (if the operation is triggered by an HTTP request).
Closing, but feel free to continue the discussion if you still have questions.
Most helpful comment
Hey @lynkas! That's not a dumb question at all, if you're new to Go's context, I'll recommend you to read more about it here.
Sorry in advance that I'm keeping it short, I don't have too much time at the moment, and I prefer to "triage" issues as fast as I can. Also, some experts may explain it better than me.
There are many examples where Go context can be useful, here are some of our use cases:
gqlgenfor supporting field collections)And there are more - like I mentioned above, I'll recommended you to read about it before.
For testing, you can
context.Background(), for real code, I'll recommend you to usehttp.Reuqest.Context(if the operation is triggered by an HTTP request).Closing, but feel free to continue the discussion if you still have questions.