1. 9

  2. 2

    to incrementally create type-safe contexts via middleware functions

    What did you mean by this? I’ve not much middleware experience, so I’m unclear why anything but the router interface would use contexts.

    1. 1

      A lot of people use contexts something like this for HTTP servers (simplified, obviously): https://play.golang.org/p/kZngCKeBt_Y – Also, uh, people have opinions on whether this is good practice or not because it’s not type-safe.

      In the playground example, I add the user ID and the logger incrementally by enriching the context with more data in two different functions (a.k.a. middlewares), but ugh, it’s not type-safe.

      But the blog post, I created one function (a.k.a. middleware) to attach both the user ID and the logger to the context. It’s arguably bad form to keep them together, as they are independent pieces of logic. So if one could have different functions adding those elements separately, but in a type-safe manner, then that would be neat.