Do not store Contexts inside a struct type; instead, pass a Context explicitly to each function that needs it, - documentation for context
Bad:
// WRONG
struct Bar {
ctx context.Context
}
func NewBar(ctx context.Context) *Bar {
return &Bar {
ctx: ctx,
}
}
func (s *Bar) Foo() {
// Refering to s.ctx
}
Good:
func (s *Bar) Foo(ctx context.Context) {
// use ctx
}
<aside> 👉
Here’s a good article to read: https://zenhorace.dev/blog/context-control-go/
</aside>