Documentation ¶
Overview ¶
Un-marshaling environment variables to Go structs
Getting Started ¶
Let's set a bunch of environment variables and then run your go app
#!/usr/bin/env bash export DEBUG="false" export DB_HOST="localhost" export DB_PORT="8012" ./your_go_app
Within your Go app do
import "github.com/tomazk/envcfg" // declare a type that will hold your env variables type Cfg struct { DEBUG bool DB_PORT int DB_HOST string } func main() { var config Cfg envcfg.Unmarshal(&config) // config is now set to Config{DEBUG: false, DB_PORT: 8012, DB_HOST: "localhost"} // optional: clear env variables listed in the Cfg struct envcfg.ClearEnvVars(&config) }
More documentation in README: https://github.com/tomazk/envcfg
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClearEnvVars ¶
func ClearEnvVars(v interface{}) error
ClearEnvVars will clear all environment variables based on the struct field names or struct field tags. It will keep all those with envcfgkeep:"" struct field tag. It will return an error, if it recieves an unsupported non-struct type, if types of the fields are not supported
func Unmarshal ¶
func Unmarshal(v interface{}) error
Unmarshal will read your environment variables and try to unmarshal them to the passed struct. It will return an error, if it recieves an unsupported non-struct type, if types of the fields are not supported or if it can't parse value from an environment variable, thus taking care of validation of environment variables values.
Types ¶
type EnvUnmarshaler ¶
type EnvUnmarshaler struct {
// contains filtered or unexported fields
}
EnvUnmarshaler holds unmarshaling settings and implements the Unmarshaler interface
func NewUnmarshaler ¶
func NewUnmarshaler() EnvUnmarshaler
NewUnmarshaler returns new EnvUnmarshaler
func (EnvUnmarshaler) FailOnUndefined ¶
func (e EnvUnmarshaler) FailOnUndefined() EnvUnmarshaler
FailOnUndefined will cause Unmarshal to fail if an environment variable is not defined instead of setting it to the types default value
func (EnvUnmarshaler) Unmarshal ¶
func (e EnvUnmarshaler) Unmarshal(v interface{}) error
Unmarshal will read your environment variables and try to unmarshal them to the passed struct. It will return an error, if it recieves an unsupported non-struct type, if types of the fields are not supported or if it can't parse value from an environment variable, thus taking care of validation of environment variables values.
type Unmarshaler ¶
type Unmarshaler interface {
Unmarshal(interface{}) error
}
Unmarshaler will unmarshal environment variables into the given struct