A low dependency package for .env files.
go get github.com/h-dav/envconfig
Create your config structure:
type Config struct {
LogLevel string `env:"LOG_LEVEL" default:"info"`
Server struct {
Port string `env:"PORT" required:"true"`
} `prefix:"SERVER_"`
}
Then just read pass your config structure along with the path to your .env file to envconfig.Set:
var cfg Config
err := envconfig.Set("./config/default.env", &cfg)
The corresponding .env file for this example:
LOG_LEVEL: debug
SERVER_PORT: 8080
required
:true
orfalse
default
: Fall back value if environment variable is not set.prefix
: Used for nested structures.- Text Replacement:
${EXAMPLE}
can be used to insert other environment variables.
- string
- int
- float
- bool
- []string
- []int
- []float
Note
This package takes heavy inspiration from httputil for handling reflection.