A helper for that - admittedly very rare - YAML/JSON scenario where you may want a single key->value pair represented as a map, but don't want to have map[string]interface{}
strewn throughout your code
see example.go
My use case was I wanted to represent environment variables in a config file, BUT there were caveats to the easy methods:
- if I used
Environment map[string]interface{}
their order would be randomized, precluding logical self-referencing env varsenvironment: foo: bar baz: bingle
- if I used
Environment []map[string]interface{}
you get an edge case where one can have too many entries in an env varenvironment: - foo: bar - baz: bingle chicken: soup # this could easily be entered by accident, but is an error and would require a lot of excess checking
- also notable that the use of
map[string]interface{}
would be highly ambiguous, even with aliasing
- also notable that the use of
- a common solution seen in YAML formats is to just curtail the above conundra and parse env vars separately:
But I've always kind of hated that, so - whisky in hand - sought alternatives
environment: - foo=bar - baz=bingle
By going admittedly overboard and building this type, we get:
- type safety in application code
- useful marshal/unmarshal checking in a single place
- vaguely self-descriptive application code
*[conundra]: ie. conundrums - whilst there's a fairly scathing remark about people who say "conundra" here I still use it https://english.stackexchange.com/a/556467