Package load-env implements loading value from environment variables.
Use go get
go get github.com/amovah/load-env
First you need to define a struct with load-env tags:
type Config struct {
Port int `env:"name=PORT,default=8080"`
Host string `env:"name=HOST,required"`
}
Then simply call LoadEnv on your struct:
func main() {
config := Config{}
load.LoadEnv(&config)
fmt.Println(config)
}
- name: The name of the environment variable.
- default: The default value if the environment variable is not set.
- required: If the environment variable is not set, the function returns an error.