this post was submitted on 15 May 2025
1 points (100.0% liked)

Golang

2674 readers
1 users here now

This is a community dedicated to the go programming language.

Useful Links:

Rules:

founded 2 years ago
MODERATORS
 

I'm working on a rather fresh project and that makes me question stuff I take for granted in existing projects. One of those things is how we access context. In existing projects, we use functions like "foobarFromContext(ctx)" that grab foobar from context for us. Writing to context usually happens in middleware.

When I was tinkering with deno + hono, context was frequently accessed directly instead of get funcs and it felt way more volatile? and I liked that.

I try to look at context as a "dynamic constants container", so stuff that is probably permanent for the lifespan of the context, e.g. the authorized user that made the call. So keep writing to context in middlewares is less of a problem for me, but get-funcs that have to be injected to each and every service and mocked in tests for a simple "ctx.Value("userName") sometimes seems so overengineered and makes ctx feel like some distant, transcendend thing.

What's your way?

no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here