Environment variables (envs) are a common simple way to store named data. They are represented as a dictionary and have a syntactic entry:
<key>=<value>
where the key and value are strings (which is determined by the OS).
They are supplied to the program through the entry point (often the main()
function) along with the startup arguments.
What about other env sources?
The source of environment variables can also be a file with syntactically correct records separated by a separator. And along with the environment variables supplied by the OS, it can be loaded at runtime. However, in both cases we get string data (in the best case, a string-string map)
Unmarshalling & Marshaling
Unmarshaling itself allows you to disassemble something and build your own. This is useful when, for example, a string contains structured data and you want to use structures with typed fields bypassing parsing and type casting.
Basically, it's a serialization and deserialization mechanism in Golang that uses reflection.
encoding/json
lib
The json package implements JSON encoding and decoding as defined in RFC 7159, with support for marshalling and unmarshaling (i.e. deserializing JSON to Go and back)...
Env's Unmarshalling using Json
A set of environment variables is indeed a string mapping. It is also true that any string mapping can be serialized into a JSON object. And then it is deserialized into a Go object, which is often forgotten and libraries are implemented with their own serialization and deserialization.
As for the overhead, this practice is often a one-time thing that happens during service startup, so serializing the map to JSON and deserializing it to a Go object does not slow down the system as a whole.
An example of this approach can be found on GitHub: enve
The implementation reuses already available components with an emphasis on implementing its own environment variable map sources.
Conclusion
The json library allows you to implement deserialization of a set of environment variables in a short time, which allows you to reduce costs without losing functionality.
Top comments (0)