Simple configuration library that binds values to environment variables.
Declaring your configuration objects, optionally decorating each field with struct tags to control the variable binding:
type Config struct {
Port int `env:"PORT" default:"8080"`
Debug bool `default:"false"` // Will use the all-caps name "DEBUG"
Database url.URL `env:"DATABASE_URL"`
}Then, bind the struct to set field values based on the current state of environment variables.
var cfg Config
if err := vary.Bind(&cfg); err != nil {
log.Fatal(err)
}If you want to use an application specific environment variable prefix in order to avoid collisions, you can configure the prefix before binding:
vary.SetPrefix("MYAPP")
// This will look for MYAPP_PORT, MYAPP_DEBUG, and MYAPP_DATABASE_URL,
// falling back to the base name only when the one with the prefix is not set.
vary.Bind(cfg)Refer to the documentation for more information on prefixes and how to control the order of precedence between names.