Our common application configuration requires each application to define a small set of descriptive information about the service in order to make the application deployable, discoverable, etc.
envconfig.Process along with default struct tag definitions allow for both global default value configuration as well as environment-specific configuration. However, they do not support defining application-specific defaults for common configuration concerns (application name, e.g.) while preserving global default values and required field behaviors. It's not possible to use default value and setting the same information in each environment is redundant and prone to errors when not setting it correctly in each environment.
envconfig.Overlay enables per-application defaults by replacing only values defined in a configuration struct instance with environment variables and not the default values (if they are defined).
Configuration values will adhere to following order of precedence with these changes:
Use Case
Our common application configuration requires each application to define a small set of descriptive information about the service in order to make the application deployable, discoverable, etc.
envconfig.Process
along withdefault
struct tag definitions allow for both global default value configuration as well as environment-specific configuration. However, they do not support defining application-specific defaults for common configuration concerns (application name, e.g.) while preserving global default values and required field behaviors. It's not possible to usedefault
value and setting the same information in each environment is redundant and prone to errors when not setting it correctly in each environment.envconfig.Overlay
enables per-application defaults by replacing only values defined in a configurationstruct
instance with environment variables and not the default values (if they are defined).Configuration values will adhere to following order of precedence with these changes:
Configuring a simple web server
Assume we have the provided configuration struct:
with the following environment variables defined:
We can define the server info before overlaying values from the environment as follows:
Then, we run
envconfig.Overlay()
and expectinfo
to be represented in json as: