4 ms·
If you use context.Context for this you give up a lot of type safety and generally make your data passing opaque. It's totally fine to put multiple values into
by haiku2077 1y ago
If you use context.Context for this you give up a lot of type safety and generally make your data passing opaque.
It's totally fine to put multiple values into a different data bag type that has explicit, typed fields. For example, the Echo framework has its own strongly typed and extensible Context interface for request scoped data: https://pkg.go.dev/github.com/labstack/echo#Context https://pkg.go.dev/github.com/labstack/echo#Context
- the_mitsuhiko 1y ago> If you use context.Context for this you give up a lot of type safety and generally make your data passing opaque. The data passing maybe, not sure how you lose type safety. The value comes from the context with the right type just fine. The stuff that I'm attaching to the context are effectively globals just that this way you can enable proper isolation in tests and else. From my limited experience with echo, the context there is not at all the same thing.
- haiku2077 1y agoContext.Value's signature is Value(any) any - you have to use type conversion or reflection to determine the value's type at runtime, instead of a compile-time check.
- the_mitsuhiko 1y agoBut I have methods such as MustRateLimiterFromContext(ctx) which returns the right type :)
- haiku2077 1y agoBy crashing your program at runtime if any wrong type is added!