go-external-config lets you externalize your configuration so that you can work with the same application code in different environments. You can use a variety of external configuration sources including properties files, YAML files, environment variables, and command-line arguments.
Property values can be injected directly into your code by using the env.Value function, or be bound to structured objects through env.ConfigurationProperties
go-external-config uses a very particular PropertySource order that is designed to allow sensible overriding of values. Later property sources can override the values defined in earlier ones.
Sources are considered in the following order:
- Config data (such as application.properties files).
- OS environment variables.
- Command line arguments.
Config data files are considered in the following order:
- Application properties (application.properties and YAML variants).
- Profile-specific application properties (application-{profile}.properties and YAML variants).
It is recommended to stick with one format for your entire application. If you have configuration files with both .properties and YAML format in the same location, .properties takes precedence.
To provide a concrete example, suppose you develop a component that uses a name property, as shown in the following example:
name := env.Value[string]("${name}")For container-managed beans, use the value field tag, see go-beans
name string `value:"${name}"`By default, go-external-config converts any command line option arguments (that is, arguments starting with -- (or -), such as --server.port=9000) to a property and adds them to the Environment. As mentioned previously, command line properties always take precedence over file-based property sources.
go-external-config will automatically find and load application.properties and application.yaml files from the following locations when your application starts:
- The current directory
- The
config/subdirectory in the current directory
The list is ordered by precedence (with values from lower items overriding earlier ones). Documents from the loaded files are added as PropertySource instances to the Environment.
If you do not like application as the configuration file name, you can switch to another file name by specifying a config.name property (or CONFIG_NAME environment variable). For example, to look for myproject.properties and myproject.yaml files you can run your application as follows:
go run ./cmd/myproject/ --config.name=myprojectYou can also refer to an explicit location by using the config.location property (or CONFIG_LOCATION environment variable). This property accepts a comma-separated list of one or more locations to check.
The following example shows how to specify two distinct files:
go run ./cmd/myproject/ --config.location=\
default.properties,\
override.propertiesLocations are optional and application will not fail if they do not exist.
If config.location contains directories (as opposed to files), they should end in /. At runtime they will be appended with the names generated from config.name before being loaded. Files specified in config.location are imported directly.
Both directory and file location values are also expanded to check for profile-specific files. For example, if you have a
config.locationofmyconfig.properties, you will also find appropriatemyconfig-<profile>.propertiesfiles are loaded.
In most situations, each config.location item you add will reference a single file or directory. Locations are processed in the order that they are defined and later ones can override the values of earlier ones.
If you have a complex location setup, and you use profile-specific configuration files, you may need to provide further hints so that go-external-config knows how they should be grouped. A location group is a collection of locations that are all considered at the same level. Items within a location group should be separated with ;. See the example in the Profile Specific Files section for more details.
Locations configured by using config.location replace the default locations. If you prefer to add additional locations, rather than replacing them, you can use config.additional-location property (or CONFIG_ADDITIONALLOCATION environment variable). Properties loaded from additional locations can override those in the default locations but not those in config.location.
This search ordering lets you specify default values in one configuration file and then selectively override those values in another. You can provide default values for your application in application.properties (or whatever other basename you choose with config.name) in one of the default locations. These default values can then be overridden at runtime with a different file located in one of the custom locations.
As well as application property files, go-external-config will also attempt to load profile-specific files using the naming convention application-{profile}. For example, if your application activates a profile named prod and uses YAML files, then both application.yaml and application-prod.yaml will be considered.
Profile-specific properties are loaded from the same locations as standard application.properties, with profile-specific files always overriding the non-specific ones. If several profiles are specified, a last-wins strategy applies. For example, if profiles prod,live are specified by the profiles.active property (or PROFILES_ACTIVE environment variable), values in application-prod.properties can be overridden by those in application-live.properties.
The last-wins strategy applies at the location group level. A config.location of cfg/,ext/ will not have the same override rules as cfg/;ext/.
For example, continuing our prod,live example above, we might have the following files:
cfg
application-live.properties
ext
application-live.properties
application-prod.properties
When we have a config.location of cfg/,ext/ we process all cfg files before all ext files:
cfg/application-live.properties
ext/application-prod.properties
ext/application-live.properties
When we have cfg/;ext/ instead (with a ; delimiter) we process cfg and ext at the same level:
ext/application-prod.properties
cfg/application-live.properties
ext/application-live.properties
Application properties may import further config data from other locations using the config.import property. Imports are processed as they are discovered, and are treated as additional documents inserted immediately below the one that declares the import.
For example, you might have the following in your application.properties file:
application.name=myapp
config.import=dev.propertiesThis will trigger the import of a dev.properties file in current directory (if such a file exists). Values from the imported dev.properties will take precedence over the file that triggered the import. In the above example, the dev.properties could redefine application.name to a different value.
An import will only be imported once no matter how many times it is declared.
Imports may be specified as fixed or import relative locations. A fixed location always resolves to the same underlying resource, regardless of where the config.import property is declared. An import relative location resolves relative to the file that declares the config.import property.
A location starting with a forward slash (/) is considered fixed. All other locations are considered import relative.
As an example, say we have a /demo directory containing our application file. We might add a /demo/application.properties file with the following content:
config.import=core/core.propertiesThis is an import relative location and so will attempt to load the file /demo/core/core.properties if it exists.
If /demo/core/core.properties has the following content:
config.import=extra/extra.propertiesIt will attempt to load /demo/core/extra/extra.properties. The extra/extra.properties is relative to /demo/core/core.properties so the full directory is /demo/core/ + extra/extra.properties.
The order an import is defined inside a single document within the properties/yaml file does not matter. For instance, the two examples below produce the same result:
config.import=my.properties
my.property=valueand
my.property=value
config.import=my.propertiesIn both of the above examples, the values from the my.properties file will take precedence over the file that triggered its import.
Several locations can be specified under a single config.import key. Locations will be processed in the order that they are defined, with later imports taking precedence.
Profile resolution does not happen for import. The example above would import direct resource
my.propertiesand nomy-<profile>.propertiesvariants. Directory import is not supported.
Some cloud platforms cannot add a file extension to volume mounted files. To import these extensionless files, you need to give go-external-config a hint so that it knows how to load them. You can do this by putting an extension hint in square brackets.
For example, suppose you have a /etc/config/myconfig file that you wish to import as yaml. You can import it from your application.properties using the following:
config.import=/etc/config/myconfig[.yaml]go-external-config uses Go's standard log/slog API. Logging levels
are controlled by the application's slog configuration.
Enable INFO logging to see which configuration files were loaded. (See also go-log4g)
Go runs tests with the package directory as the working directory rather
than the module root. go-external-config automatically detects the Go test runner and
locates the module root by searching parent directories for go.mod.
When running tests, default configuration is looked up in:
- The module directory
- The
config/subdirectory of the module directory
When running applications on a cloud platform (such as Kubernetes) you often need to read config values that the platform supplies. Assume there’s an environment variable called CLUSTER:
my.name=Service1
my.cluster=${CLUSTER}Most operating systems impose strict rules around the names that can be used for environment variables. For example, Linux shell variables can contain only letters (a to z or A to Z), numbers (0 to 9) or the underscore character (_). By convention, Unix shell variables will also have their names in UPPERCASE.
go-external-config’s relaxed binding rules are, as much as possible, designed to be compatible with these naming restrictions.
To convert a property name in the canonical-form to an environment variable name you can follow these rules:
- Replace dots (
.) with underscores (_). - Remove any dashes (
-). - Convert to uppercase.
For example, the configuration property main.log-startup-info would be an environment variable named MAIN_LOGSTARTUPINFO.
Environment variables can also be used when binding to object lists. To bind to a List, the element number should be surrounded with underscores in the variable name.
For example, the configuration property my.service[0].other would use an environment variable named MY_SERVICE_0_OTHER.
The values in application.properties and application.yaml are filtered through the existing Environment when they are used, so you can refer back to previously defined values (for example, from environment variables). The standard ${name} property-placeholder syntax can be used anywhere within a value. Property placeholders can also specify a default value using a : to separate the default value from the property name, for example ${name:default}.
The use of placeholders with and without defaults is shown in the following example:
app.name=MyApp
app.description=${app.name} is an application written by ${username:Unknown}Assuming that the username property has not been set elsewhere, app.description will have the value MyApp is an application written by Unknown.
You can also use this technique to create “short” variants of existing properties. Some people like to use (for example) --port=9000 instead of --server.port=9000 to set configuration properties on the command line. You can enable this behavior by using placeholders in application.properties, as shown in the following example:
server.port=${port:8080}go-external-config provides support for expr-lang. Consider the following example:
server.threads.max=200
# keepalive threads should remain below total worker threads
server.threads.keepalive=#{${server.threads.max} / 4}go-external-config provides the hook point necessary to modify values contained in the Environment. You can load property from external locations, for example AWS Systems Manager Parameter Store etc.
Base64PropertySource is useful for decoding property values in Base64 format as shown in the following example:
hidden=base64:aGlkZGVuThe RandomValuePropertySource is useful for injecting random values (for example, into secrets or test cases). It can produce int, int64, uuids, or strings, as shown in the following example:
my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.int64}
my.uuid=${random.uuid}
my.number-less-than-ten=${random.int(10)}
my.number-in-range=${random.int(1024,65536)}The CachedPropertySource is useful for caching dynamically resolved values for the lifetime of the application. Values prefixed with cached: are resolved only once per property key and then reused for all subsequent lookups.
This is particularly useful when combining generators such as random.* with values that should remain stable during the application run, for example application instance identifiers, node identifiers, or ephemeral secrets generated at startup.
node.id=cached:${random.uuid}In the example above, property is resolved only once. Every subsequent lookup of node.id returns the same value.
Properties without the cached: prefix retain their original behavior. For example:
request.id=${random.uuid}produces a new UUID on every lookup, which is useful for values that are expected to be unique, such as request, message, or task identifiers.
| Extension | Description |
|---|---|
| AWS | Resolve properties from AWS Systems Manager Parameter Store and AWS Secrets Manager. |
| Vault | Resolve properties from HashiCorp Vault. |
| Consul | Resolve properties from HashiCorp Consul. |
| RSA | Decrypt RSA-encrypted configuration properties. |
Extensions are enabled simply by importing their packages.
For example, to enable AWS Parameter Store and Secrets Manager support:
import _ "github.com/go-external-config/aws"The extension automatically registers its property sources during Go package initialization. No explicit registration code is required.
Once registered, extensions participate in the standard property resolution process. Refer to each extension's documentation for its configuration requirements and supported property syntax.
Applications can implement custom property preprocessors to intercept property lookups and provide additional resolution logic.
Property lookups can be intercepted in two ways:
- By key - recognize specific property keys or patterns and resolve them dynamically.
- By value - recognize special values returned by existing property sources and transform or resolve them.
For example, the AWS extension supports both:
aws-param.my/parameter- resolves a property directly from AWS Parameter Store.aws-param:my/parameter- resolves an AWS parameter reference stored as a property value.
Custom preprocessors implement the env.PropertySource interface:
type PropertySource interface {
Name() string
HasProperty(key string) bool
Property(key string) string
Properties() map[string]string
}Register a preprocessor using env.RegisterPropertySource():
env.RegisterPropertySource(NewMyPropertyPreprocessor())YAML is a superset of JSON and, as such, is a convenient format for specifying hierarchical configuration data. The go-external-config automatically supports YAML as an alternative to properties.
YAML documents need to be converted from their hierarchical format to a flat structure that can be used with the Environment. For example, consider the following YAML document:
environments:
dev:
url: "https://dev.example.com"
name: "Developer Setup"
prod:
url: "https://another.example.com"
name: "My Cool App"In order to access these properties from the Environment, they would be flattened as follows:
environments.dev.url=https://dev.example.com
environments.dev.name=Developer Setup
environments.prod.url=https://another.example.com
environments.prod.name=My Cool AppLikewise, YAML lists also need to be flattened. They are represented as property keys with [index] dereferencers. For example, consider the following YAML:
my:
servers:
- "dev.example.com"
- "another.example.com"The preceding example would be transformed into these properties:
my.servers[0]=dev.example.com
my.servers[1]=another.example.comUsing env.Value[string]("${property}") to inject configuration properties can become cumbersome when working with multiple properties or hierarchical configuration. go-external-config provides an alternative approach that binds configuration properties directly to strongly typed Go structs.
For example:
var db struct {
Host string
Port int
User string
Pass string
}
env.ConfigurationProperties("db", &db)Property values are automatically converted to the corresponding Go field types during binding. In this example, Port is converted to an int, while the remaining fields are populated as strings. The same automatic type conversion applies when resolving individual properties using env.Value[T]().
Host value will be looked-up in
db.Hostanddb.hostproperties
Example of initializing http.Client and redis.Client with shared default properties and overrides for the specific component:
var httpClient *http.Client = env.ConfigurationProperties("component.httpClient", env.ConfigurationProperties("httpClient", &http.Client{
Transport: env.ConfigurationProperties("component.httpClient.transport", env.ConfigurationProperties("httpClient.transport", &http.Transport{}))}))
var redisClient *redis.Client = redis.NewClient(env.ConfigurationProperties("component.redis", env.ConfigurationProperties("redis", &redis.Options{})))httpClient:
timeout: '#{30 * time.Second}'
transport:
maxIdleConns: 100
maxIdleConnsPerHost: 10
idleConnTimeout: '#{90 * time.Second}'
redis:
addr: ${REDIS_HOST}:6379
password: ${REDIS_PASSWORD}
poolSize: 5
minIdleConns: 1
connMaxIdleTime: '#{5 * time.Minute}'
dialTimeout: '#{5 * time.Second}'
readTimeout: '#{3 * time.Second}'
writeTimeout: '#{3 * time.Second}'
component:
httpClient:
timeout: '#{120 * time.Second}'
redis:
poolSize: 50
readTimeout: '#{10 * time.Second}'
writeTimeout: '#{30 * time.Second}'
...The resulting clients inherit the shared configuration, with component-specific properties taking precedence. For example, component.redis.poolSize overrides redis.poolSize, while redis.minIdleConns retains its original value.
go-external-config provides a way to segregate parts of your application configuration and make it be available only in certain environments. Any Bean can be created with Profile to limit when it is loaded, as shown in the following example (go-beans dependency required):
ioc.Bean[NotificationSender]().Profile("kafka").Factory(NewKafkaNotificationSender).Register()
ioc.Bean[NotificationSender]().Profile("rabbitmq").Factory(NewRabbitMqNotificationSender).Register()You can use a profiles.active Environment property to specify which profiles are active. You can specify the property in any of the ways described earlier in this chapter. For example, you could include it in your application.properties, as shown in the following example:
profiles.active=dev,hsqldbThe first profiles.active value found in configuration files is used. It can be overridden by the PROFILES_ACTIVE environment variable, the --profiles.active command-line argument, or profiles set programmatically with env.SetActiveProfiles.
Additional profiles can be activated using the profiles.include property:
profiles.include=kubernetesUnlike profiles.active, profiles.include values from configuration files are cumulative. Included profiles are added after the active profiles and follow the same last-wins ordering used for profile-specific configuration files.
Included profiles are added after the active profiles and therefore follow the same last-wins ordering used for profile-specific configuration files. For example:
profiles.active=prod
profiles.include=kubernetesloads profile-specific configuration in the following order:
application-prod.properties
application-kubernetes.properties
Values from application-kubernetes.properties can therefore override values from application-prod.properties.
Profiles can also be included from profile-specific configuration. For example:
# application-prod.yaml
profiles:
include: kubernetesActivating the prod profile also activates the kubernetes profile and loads application-kubernetes.yaml.
Includes are resolved transitively. If application-kubernetes.yaml includes another profile, its profile-specific configuration is loaded as well. Duplicate and empty profile names are ignored.
You can programmatically set active profiles by calling env.SetActiveProfiles("...") before your application runs. This can be useful for tests to mock Beans or other scenarios.
Spring Externalized Configuration
go get github.com/go-external-config/go