A KISS way to deal with environment variables in Go.
A very basic example (check the examples
folder):
package main
import (
"fmt"
"time"
"github.com/caarlos0/env"
)
type config struct {
Home string `env:"HOME"`
Port int `env:"PORT" envDefault:"3000"`
IsProduction bool `env:"PRODUCTION"`
Hosts []string `env:"HOSTS" envSeparator:":"`
Duration time.Duration `env:"DURATION"`
TempFolder string `env:"TEMP_FOLDER" envDefault:"${HOME}/tmp" envExpand:"true"`
}
func main() {
cfg := config{}
err := env.Parse(&cfg)
if err != nil {
fmt.Printf("%+v\n", err)
}
fmt.Printf("%+v\n", cfg)
}
You can run it like this:
$ PRODUCTION=true HOSTS="host1:host2:host3" DURATION=1s go run examples/first.go
{Home:/your/home Port:3000 IsProduction:true Hosts:[host1 host2 host3] Duration:1s}
The library has built-in support for most types and their respectives slices, but you can also use/define a custom parser func for any other type you want.
If you set the envDefault
tag for something, this value will be used in the
case of absence of it in the environment. If you don't do that AND the
environment variable is also not set, the zero-value
of the type will be used: empty for string
s, false
for bool
s,
0
for int
s and so forth.
By default, slice types will split the environment value on ,
; you can change
this behavior by setting the envSeparator
tag.
If you set the envExpand
tag, environment variables (either in ${var}
or
$var
format) in the string will be replaced according with the actual value
of the variable.
If you have a type that is not supported out of the box by the lib, you are able
to use (or define) and pass custom parsers (and their associated reflect.Type
)
to the env.ParseWithFuncs()
function.
In addition to accepting a struct pointer (same as Parse()
), this function
also accepts a env.CustomParsers
arg that under the covers is a
map[reflect.Type]env.ParserFunc
.
To see what this looks like in practice, take a look at the commented block in the example.
env
also ships with some pre-built custom parser funcs for common types. You
can check them out here.
The env
tag option required
(e.g., env:"tagKey,required"
) can be added
to ensure that some environment variable is set. In the example above,
an error is returned if the config
struct is changed to:
type config struct {
Home string `env:"HOME"`
Port int `env:"PORT" envDefault:"3000"`
IsProduction bool `env:"PRODUCTION"`
Hosts []string `env:"HOSTS" envSeparator:":"`
SecretKey string `env:"SECRET_KEY,required"`
}