Documentation
¶
Overview ¶
Package gopts provide facilities to load environment variables into a struct.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoadEnvs ¶
func LoadEnvs(baseObj interface{}) interface{}
LoadEnvs is an alias to `LoadEnvsWithPrefix("", baseObj)`
func LoadEnvsWithPrefix ¶
func LoadEnvsWithPrefix(prefix string, baseObj interface{}) interface{}
LoadEnvsWithPrefix loads data from environment variables into a managed provided by you. Useful for loading settings stored in the OS environment.
The following types are currently supported:
- bool
- int
- int8
- int16
- int32
- int64
- []string
- string
gopts will use fields of the provided object to match environment keys. For instance, it expects that a field named APIKey is available as `API_KEY` in the environment. Assuming the prefix argument is provided with a non-empty value "PREFLIGHT", the library will look for a env named PREFLIGHT_API_KEY.
Setting a gopts:"-" tag prevents gopts from filling the associated field.
A "default" tag may also be set. Its value will be set to the field in case it is absent from the environment vars set.
For instance, take the following struct:
type Settings struct {
Username string
SecretKey string `default:"s3cr37"`
AutoRestart bool `default:"true"`
IgnoredField string `gopts:"-"`
}
and the following environment variables:
- PREF_USERNAME=Rob
- PREF_AUTO_RESTART=false
running the following snippet:
settings := gopts.LoadEnvsWithPrefix("pref", Settings{}).(Settings)
will yield the following result:
{
Username: "Rob",
SecretKey: "s3cr37",
AutoRestart: false,
IgnoredField: ""
}
Types ¶
This section is empty.