Documentation
¶
Overview ¶
Package simpleenv provides a simple way to configure your application environment variables using struct tags
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Load ¶
Load loads environment variables into the given struct and validates the constraints specified in the struct tags
e.g. `env:"ENVIRONMENT;oneof=development,test,staging,production"`
will load the environment variable `ENVIRONMENT` and validate
that it is one of the values in the `oneof` constraint.
valid constraints:
- optional: the environment variable may be missing
- allowempty: only for string or text unmarshaler fields; allows KEY="" when present
- oneof: the environment variable must be one of the values in the `oneof` constraint list (separeted by commas)
- min: the environment variable must be greater than or equal to the value in the `min` constraint
- max: the environment variable must be less than or equal to the value in the `max` constraint
- regex: the environment variable must match the regex pattern in the `regex` constraint
- format: the environment variable must match the format in the `format` constraint
supported formats: URL, URI, FILE, DIR, HOSTPORT, UUID, IP, HEX, ALPHANUMERIC, IDENTIFIER
note: only one format value is supported (e.g. `format=URL`)
supported field types:
- string
- bool
- int
- int64
- uint
- float64
- time.Duration
- custom types implementing encoding.TextUnmarshaler
example:
type AppEnv struct {
Environment string `env:"ENVIRONMENT;oneof=development,test,staging,production"`
Version float64 `env:"VERSION;"`
ApiURL string `env:"API_URL;;format=URL"`
Concurrency int `env:"CONCURRENCY;optional;min=1"`
}
appEnv := AppEnv{}
appenv.Load(&appEnv)
appEnv.Environment // "development"
Make sure to pass a non-nil pointer to a struct (for example: &cfg), otherwise Load returns an input validation error. Load also returns an error if required environment variables are not set or if any value does not match the constraints.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.