Custom Http Handlers Part 2

In the last post I covered a way to pass data to http handlers without using context.WithValue(). I saw another interesting way to do a type of dependency injection in a package from Jeremy called Mixer. If you own the server implementation and don’t want to import a 3rd party package you can do something similar. This doesn’t rely on generics so it is backwards compatible with older versions of Go....

October 8, 2022 · 6 min · 1108 words · John Hooks

Custom Http Handlers and context.WithValue()

One thing I try to avoid when I can is using the context.WithValue(). It’s a black box map of map[interface{}]interface{}. This is obviously flexible because anything can be stored here but there are pitfalls. The documentation states you need to create your own types for keys: The provided key must be comparable and should not be of type string or any other built-in type to avoid collisions between packages using context....

September 28, 2022 · 6 min · 1119 words · John Hooks