So, I have a context.Context(https://golang.org/pkg/context/) variable with me, is there a way I can list all the keys this variable holds?
No there is no way to list all the keys of context.Context
. Because that type is just an interface. So what does this mean?
In general a variables can hold a concrete type or an interface. A variable with an interface type does not have any concrete type informations on it. So it would makes no difference if the interface is empty (interface{}
) or context.Context. Because they could be a lot of different types which are implementing that interface. The variable does not have a concrete type. It is just something abstract.
If you use reflection you could observer the fields and all the methods of the type which is set to that variable (with interface type). But the logic how the method Value(key interface{}) interface{}
is implemented is not fixed. It does not have to be a map. You could also make an implementation with slices, database, an own type of an hash table, ...
So there is no general way to list all the values.