Khan Academy Engineering Blog Post:
https://blog.khanacademy.org/statically-typed-context-in-go/Khan Academy has been porting our backend from Python to Go. One thing we hoped to get from it was fewer uses of implicit global resources and request data. We looked at explicitly passing everything as parameters, or hiding things in the context.
In the end we decided to extend the context interface to provide access to these global & request elements. This gives us strongly typed interfaces, with less verbosity than writing every parameter out.
func DoTheThing(
ctx interface {
context.Context
RequestContext
DatabaseContext
HttpClientContext
SecretsContext
LoggerContext
},
thing string,
) {...}
I've written a whole blog post about the idea, our motivations, and our experience with it. There's also some sample code available showing the different approaches we considered:
https://github.com/Khan/typed-context/I realize this is not a wholly uncontroversial idea, but I'd appreciate any feedback.
Adam