Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotAPointer = errors.New("not a pointer") ErrTimeLayoutRequired = errors.New("missing timeLayout tag") ErrRequiredField = errors.New("required field") ErrEmptyField = errors.New("empty field") )
List of injection errors.
Functions ¶
func LoadAndParse ¶
func LoadAndParse(st interface{}) error
LoadAndParse convenience function which first loads environment variables and then injects them into the given struct.
This simplifies the following use case:
if err := dotenv.Load(); err != nil {
panic(err)
}
env := Config{}
if err := dotenv.Parse(&env); err != nil {
panic(err)
}
The above code is equivalent to:
env := Config{}
if err := dotenv.LoadAndParse(); err != nil {
panic(err)
}
See Parse function for more information.
func MustLoadAndParse ¶ added in v0.0.3
func MustLoadAndParse(st interface{})
MustLoadAndParse convenience function which calls LoadAndParse and panics if an error is returned.
func MustParse ¶ added in v0.0.3
func MustParse(st interface{})
MustParse convenience function which calls Parse and panics if an error is returned.
func Parse ¶
func Parse(st interface{}) error
Parse injects environment variables into the given struct using tag annotations.
The given argument must be a pointer to a struct where values will be injected.
Struct must tag its fields with `env:"VAR_NAME"` to specify the environment variable value to be injected.
Fields may be marked as required using the `required` env option. If a field is required and no environment variable is found, an error will be returned.
However, if you want to make sure that a field is not empty, you can use the `notEmpty` option. In which case an error will be returned if not value is found.
Time fields:
Optionally, the tag `default` may be used to specify a default value for the field. In the case of `time.Time` fields, the `timeLayout` tag must be used to specify the format of the time string.
String slices:
Optionally, the tag `delimiter` may be used to specify a separator for string slices, by default `,` will be used.
For example:
type Config struct {
Foo string `env:"ENV_FOO,required" default:"fooValue"`
Bar int `env:"ENV_BAR,notEmpty"`
IPs []string `env:"ENV_IPS" delimiter:";"`
When time.Time `env:"ENV_WHEN" default:"2021-12-24T17:04:05Z07:00" timeLayout:"2006-01-02T15:04:05Z07:00"`
}
Fields without an `env` tag will not be injected.
func WithOverride ¶
func WithOverride(callback func(), kv ...string)
WithOverride overrides the environment variables with the given ones and restores them after the callback is executed.
Any call to the Parse, LoadAndParse or similar within the callback will be affected by the overridden values.
This function will panic if the number of arguments is not even, or if there is an error setting or unsetting the environment variables.
Typical Usage Example:
dotenv.WithOverride(func() {
functionThatCallsLoadAndParse()
}, "FOO", "bar")
Types ¶
This section is empty.