Tiny environment variable mapper for golang
- Go 1.11+
$ go get -u github.com/akito0107/envconf
set environment variables.
export DB_HOST=localhost
export DB_PORT=5432
and implement Go code like this
package main
import (
"log"
"github.com/akito0107/envconf"
)
// struct with `env` tagged literal
type Config struct {
DBHost string `env:"DB_HOST"` //
DBPort int `env:"DB_PORT"`
}
func main() {
var conf Config
// load environment variables with `Load` method
if err := envconf.Load(&conf); err != nil {
log.Fatal(err)
}
log.Printf("%+v\n", conf) // should be `{DBHost:localhost DBPort:5432}`
}
You can confirm that variables are mapped to struct via envconf.Load
.
$ go run main.go
2019/03/01 16:46:14 {DBHost:localhost DBPort:5432}
envconf.Load
, in default behaviour, returns error when specified environment variables are blank.
If missing DB_PORT
variable, returns error.
$ export DB_HOST=localhost
$ go run main.go
2019/03/01 16:49:56 init: relpaceEnv failed: environmentVariableNotFound Envname: DB_PORT
exit status 1
If you want to allow empty variables, you must specify allow-empty
on env
tag.
This feature convenient for setting default parameter.
// struct with `env` tagged literal
type Config struct {
DBHost string `env:"DB_HOST"`
DBPort int `env:"DB_PORT,allow-empty"` // set allow-empty
}
....
var conf Config
// load environment variables with `Load` method
if err := envconf.Load(&conf); err != nil {
log.Fatal(err) // should not be error
}
log.Printf("%+v\n", conf) // 2019/03/01 16:57:57 {DBHost:localhost DBPort:0}
conf2 := Config{DBPort: 12345} // passing default param
// load environment variables with `Load` method
if err := envconf.Load(&conf2); err != nil {
log.Fatal(err)
}
log.Printf("%+v\n", conf2) // 2019/03/01 16:57:57 {DBHost:localhost DBPort:12345}
envconf
support using with dotenv
by passing UseDotEnv option.
In default case, .env vars override environment variables
Go code.
// load environment variables and dotenv variables with `Load` method
if err := envconf.Load(&conf, envconf.UseDotEnv()); err != nil {
log.Fatal(err)
}
log.Printf("%+v\n", conf)
$ cat .env
DB_USER=test
$ export DB_HOST=localhost
$ export DB_PORT=5432
$ export DB_USER=test2
$ go run main.go
2019/03/01 17:15:31 {DBHost:localhost DBPort:5432 DBUser:test} # overrided .env
if ENVCONF_LOAD_DOTFILE
is set to disable
, skip load .env
.
This feature convenient for avoid to set undesired vars from .env
.
$ cat .env
DB_USER=test
$ export ENVCONF_LOAD_DOTFILE=disable
$ export DB_HOST=localhost
$ export DB_PORT=5432
$ export DB_USER=test2
$ go run main.go
2019/03/01 17:15:31 {DBHost:localhost DBPort:5432 DBUser:test2} # overrided .env
This project is licensed under the Apache License 2.0 License - see the LICENSE file for details