An environment variable reader for Go that bind env value into struct variable, with zero external dependency. The main idea of making this project is the use of os and reflect packages from Go.
This package will provide following features:
- Read system environtment variable bassed on
envstruct tag - Bind env value into struct
- Fill struct with default value if env not present
To install this package we can run go command:
go get -u github.com/rasatmaja/mura/v2import "github.com/rasatmaja/mura/v2"
type Config struct {
Host string `env:"SERVER_HOST" default:"localhost"`
Port int `env:"SERVER_PORT" default:"8080"`
TLS bool `env:"SERVER_TLS" default:"false"`
}
cfg := new(Config)
err := mura.Unmarshal(cfg)
if err != nil {
panic(err)
}The code above will do:
- Read environtment variable based on
envtag defined in struct - If the environment variable is found, it will fill the struct value with
envvalue - If no environment variable found, the struct field will be filled with
defaulttag's value
