Package Usage: go: github.com/rusriver/config
Package config provides convenient access methods to configuration stored as
JSON or YAML.
Let's start with a simple YAML file config.yml:
We can parse it using ParseYaml(), which will return a *Config instance on
success:
An equivalent JSON configuration could be built using ParseJson():
From now, we can retrieve configuration values using a path in dotted notation:
Besides String(), other types can be fetched directly: Bool(), Float64(),
Int(), Map() and List(). All these methods will return an error if the path
doesn't exist, or the value doesn't match or can't be converted to the
requested type.
A nested configuration can be fetched using Get(). Here we get a new *Config
instance with a subset of the configuration:
Then the inner values are fetched relatively to the subset:
For lists, the dotted path must use an index to refer to a specific value.
To retrieve the information from a user stored in the configuration above:
JSON or YAML strings can be created calling the appropriate Render*()
functions. Here's how we render a configuration like the one used in these
examples:
This results in a configuration string to be stored in a file or database.
For more more convenience it can parse OS environment variables and command line arguments.
We can also specify the order of parsing:
In case of OS environment all existing at the moment of parsing keys will be scanned in OS environment,
but in uppercase and the separator will be `_` instead of a `.`. If EnvPrefix() is used the given prefix
will be used to lookup the environment variable, e.g PREFIX_FOO_BAR will set foo.bar.
In case of flags separator will be `-`.
In case of command line arguments possible to use regular dot notation syntax for all keys.
For see existing keys we can run application with `-h`.
We can use unsafe method to get value:
There is unsafe methods, like regular, but wuth prefix `U`.
1 version
Latest release: over 2 years ago
1 dependent package
View more package details: https://packages.ecosyste.ms/registries/proxy.golang.org/packages/github.com/rusriver/config
View more repository details: http://repos.ecosyste.ms/hosts/GitHub/repositories/rusriver%2Fconfig
Dependent Repos 1
