github.com/kkyr/fig (go): Package fig loads configuration files and/or environment variables into Go structs with extra juice for validating fields and setting defaults.  Config files may be defined in yaml, json or toml format.  When you call `Load()`, fig takes the following steps:  Define your configuration file in the root of your project:  Define your struct and load it:  Pass options as additional parameters to `Load()` to configure fig's behaviour.  Do not look for any configuration file with `IgnoreFile()`.  If IgnoreFile is given then any other configuration file related options like `File` and `Dirs` are simply ignored.  File & Dirs  By default fig searches for a file named `config.yaml` in the directory it is run from. Change the file and directories fig searches in with `File()` and `Dirs()`.  Fig searches for the file in dirs sequentially and uses the first matching file.  The decoder (yaml/json/toml) used is picked based on the file's extension.  The struct tag key tag fig looks for to find the field's alt name can be changed using `Tag()`.  By default fig uses the tag key `fig`.  Fig can be configured to additionally set fields using the environment. This behaviour can be enabled using the option `UseEnv(prefix)`. If loading from file is also enabled then first the struct is loaded from a config file and thus any values found in the environment will overwrite existing values in the struct.  Prefix is a string that will be prepended to the keys that are searched in the environment. Although discouraged, prefix may be left empty.  Fig searches for keys in the form PREFIX_FIELD_PATH, or if prefix is left empty then FIELD_PATH.  A field's path is formed by prepending its name with the names of all the surrounding structs up to the root struct, upper-cased and separated by an underscore.  If a field has an alt name defined in its struct tag then that name is preferred over its struct name.  With the struct above and `UseEnv("myapp")` fig would search for the following environment variables:  Fields contained in struct slices whose elements already exists can be also be set via the environment in the form PARENT_IDX_FIELD, where idx is the index of the field in the slice.  With the config above individual servers may be configured with the following environment variable:  Note: the Server slice must already have members inside it (i.e. from loading of the configuration file) for the containing fields to be altered via the environment. Fig will not instantiate and insert elements into the slice.  Change the layout fig uses to parse times using `TimeLayout()`.  By default fig parses time using the `RFC.3339` layout (`2006-01-02T15:04:05Z07:00`).  A validate key with a required value in the field's struct tag makes fig check if the field has been set after it's been loaded. Required fields that are not set are returned as an error.  Fig uses the following properties to check if a field is set:  See example below to help understand:  A default key in the field tag makes fig fill the field with the value specified when the field is not otherwise set.  Fig attempts to parse the value based on the field's type. If parsing fails then an error is returned.  A default value can be set for the following types:  Successive elements of slice defaults should be separated by a comma. The entire slice can optionally be enclosed in square brackets:  Note: the default setter knows if it should fill a field or not by comparing if the current value of the field is equal to the corresponding zero value for that field's type. This happens after the configuration is loaded and has the implication that the zero value set explicitly by the user will get overwritten by any default value registered for that field. It's for this reason that defaults on booleans are not permitted, as a boolean field with a default value of `true` would always be true (since if it were set to false it'd be overwritten).  The required validation and the default field tags are mutually exclusive as they are contradictory.  This is not allowed:  A wrapped error `ErrFileNotFound` is returned when fig is not able to find a config file to load. This can be useful for instance to fallback to a different configuration loading mechanism.
Keywords: config-file|configuration|configuration-management|environment|environment-variables|go|golang|json|toml|yaml