July 2019
Intermediate to advanced
458 pages
12h 12m
English
The introduction of context.Context in http.Request tries to address this issue by defining a single interface that can be assigned, recovered, and used in various handlers.
The downside is that a context isn't assigned automatically to a request, and context values cannot be recycled. There should be no really good reason to do that since the context should store data that's specific to a certain package or scope, and the packages themselves should be the only ones that are able to interact with them.
A good pattern is the usage of a unique unexported key type combined with auxiliary functions to get or set a certain value:
type keyType struct{}var key = &keyType{}func WithKey(ctx context.Context, value string) context.Context ...Read now
Unlock full access