golang/go

context: relax recommendation against putting Contexts in structs

Open

#22.602 aperta il 6 nov 2017

Vedi su GitHub
 (10 commenti) (27 reazioni) (0 assegnatari)Go (19.008 fork)batch import
DocumentationNeedsInvestigationhelp wanted

Metriche repository

Star
 (133.883 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

This is a follow-on from discussion in #14660.

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.

Let's address this concern with package-level documentation.

Also see @rsc's comment at https://github.com/golang/go/issues/14660#issuecomment-200145301.

/cc @Sajmani @bradfitz @rsc

Guida contributor