Package envconf implements reading config from enviroment variables.
import "github.com/krhubert/envconfig"
package main
type Config struct {
Host string `envconf:"name,default,required"`
}
Tag:
- name - the name of env variable (if prefix is set, it will be added to name -> prefix_name)
- default - if no env variable found, the default value will be used
- required - can be set to true, then user must set this variable (required can't be combined with default)
package main
import (
"log"
"github.com/krhubert/envconf"
)
type Config struct {
Host string `envconf:"host,,true"`
Port int `envconf:"port,80"`
Timeout int `envconf:"timeout"`
}
Basic usage
func main() {
var conf Config
if err := envconf.SetValues(&conf); err != nil {
log.Fatal(err)
}
log.Printf("host=%s, port=%d, timeout=%d\n", conf.Host, conf.Port, conf.Timeout)
}
$ HOST=127.0.0.1 PORT=8080 TIMEOUT=60 ./main
host=127.0.0.1, port=8080, timeout=60
$
Application prefix can be set by creating config
func main() {
var conf Config
if err := envconf.NewConfig("app").SetValues(&conf); err != nil {
log.Fatal(err)
}
log.Printf("host=%s, port=%d, timeout=%d\n", conf.Host, conf.Port, conf.Timeout)
}
$ APP_HOST=127.0.0.1 APP_PORT=8080 APP_TIMEOUT=60 ./main
host=127.0.0.1, port=8080, timeout=60
$