Avrêbarra~
How to Retain Types of Context Values in Golang?
- Written on January 16, 2022

“Ugh, Golang context values returns interface{} and lose its type information? How to work around this…?”

We all knew about context.Context in Golang right? One of its main use is to hold some value so we can propagate that value to next processes. (new to context.Context? start here)

Let me show a bit:

Like that. Simple, right?

But have you ever wondered (or disturbed) on how it loses values’s type information? It returns our typed values as interface{} value.I wont be explaining the reason behind why it uses interface{} though, instead I’ll talk about: can we keep those type information? Yes we can!

One simple and intuitive way to do it is by adding helper struct and functions like these:

You see, that way we can safely add, mutate, or read context values while retaining the type. This works very nicely especially if you have a clear design on what values you’re trying to pass around using context between process.

We’ve been using this model all the time in our place. So far no problems, heck we even use this pattern to enable back propagatable context values (usually context values is only readable for next process but not previous right?). So I guess it’s worth a write.

Thanks for reading. Ciao~