Package envconfig will populate your config struct based on sources such as environment variables, files, etc.
go get github.com/h-dav/envconfig/v3| Option | Description |
|---|---|
WithFilepath("config/file.env") |
Use file to populate config struct. |
WithActiveProfile("dev_env") |
Provide the profile to select a specific config file. |
env: Used to determine the key of the value to use when populating config fields.required:trueorfalsedefault: Default value if environment variable is not set.prefix: Used for nested structures.envjson: Used for deserialising JSON into config.
- Text Replacement:
${EXAMPLE}can be used to insert other discovered values.
Important
When merging values, envconfig uses the following precedence:
- Flags
- Environment Variables
- Config File (provided via
WithFilepath())
func main() {
type Config struct {
Development bool `env:"DEVELOPMENT" default:"true"`
}
var cfg Config
if err := envconfig.Set(&cfg); err != nil {
panic(err)
}
}func main() {
type Config struct {
Service string `env:"SERVICE"`
}
var cfg Config
if err := envconfig.Set(&cfg, WithFilepath("internal/config/config.env")); err != nil {
panic(err)
}
}func main() {
type Config struct {
Service string `env:"SERVICE"`
}
var cfg Config
if err := envconfig.Set(
&cfg,
WithActiveProfile(os.Getenv("ACTIVE_PROFILE")),
WithFilepath("internal/config/"), // Will use file `internal/config/development.env`.
); err != nil {
panic(err)
}
}func main() {
type Config struct {
Service struct {
Name string `env:"NAME"`
Version string `env:"VERSION"`
} `prefix:"SERVICE_"`
}
var cfg Config
if err := envconfig.Set(&cfg); err != nil {
panic(err)
}
}