3 ms·
It's not a hard rule that context should not be struct fields. See https://github.com/golang/go/issues/22602 https://github.com/golang/go/issues/22602 (context:
by SPBS 3y ago
It's not a hard rule that context should not be struct fields. See https://github.com/golang/go/issues/22602 https://github.com/golang/go/issues/22602 (context: relax recommendation against putting Contexts in structs)
"Right now the context package documentation says
> Do not store Contexts inside a struct type; instead, pass a Context explicitly to each function that needs it. The Context should be the first parameter, typically named ctx: [...]
This advice seems overly restrictive. @bradfitz wrote in that issue:
> While we've told people not to add contexts to structs, I think that guidance is over-aggressive. The real advice is not to store contexts. They should be passed along like parameters. But if the struct is essentially just a parameter, it's okay. I think this concern can be addressed with package-level documentation and examples."