Documentation
¶
Overview ¶
Example (One) ¶
_ = os.Setenv("APP_HOST", "127.0.0.1") _ = os.Setenv("APP_PORT", "8080") var cfg Config if err := env.Load(&cfg); err != nil { panic(err) } fmt.Println(cfg.Host) fmt.Println(cfg.Port)
Output: 127.0.0.1 8080
Example (Three) ¶
_ = os.Setenv("APP_NESTED_URL", "http://example.com") _ = os.Setenv("APP_NESTED_PORT", "8080") _ = os.Setenv("APP_OTHER_URL", "http://other.com") _ = os.Setenv("APP_OTHER_PORT", "9090") var cfg Config2 if err := env.Load(&cfg); err != nil { panic(err) } fmt.Println(cfg.Nested.URL) fmt.Println(cfg.Nested.Port) fmt.Println(cfg.Other.URL) fmt.Println(cfg.Other.Port)
Output: http://example.com 8080 http://other.com 9090
Example (Two) ¶
_ = os.Setenv("API_HOST", "127.0.0.1") _ = os.Setenv("API_PORT", "8080") _ = os.Setenv("API_PASSWORD", "test") var cfg Config loader := env.New("API_", log.Printf) if err := loader.Load(&cfg); err != nil { panic(err) } fmt.Println(cfg.Host) fmt.Println(cfg.Port) fmt.Println(cfg.Password)
Output: 127.0.0.1 8080 test
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrStructPointer represents the error that a pointer to a struct is expected. ErrStructPointer = errors.New("must be a pointer to a struct") // ErrNilPointer represents the error that a nil pointer is received ErrNilPointer = errors.New("the pointer should not be nil") // TagName specifies the tag name for customizing struct field names when loading environment variables TagName = "env" )
Functions ¶
func Load ¶
func Load(structPtr interface{}) error
Load populates a struct with the values read from the corresponding environment variables. Load uses "APP_" as the prefix for environment variable names. It uses log.Printf() to log the data population of each struct field. For more details on how Load() works, please refer to Loader.Load().
Types ¶
type Loader ¶
type Loader struct {
// contains filtered or unexported fields
}
Loader loads a struct with values returned by a lookup function.
func New ¶
New creates a new environment variable loader. The prefix will be used to prefix the struct field names when they are used to read from environment variables.
func NewWithLookup ¶
func NewWithLookup(prefix string, lookup LookupFunc, log LogFunc) *Loader
NewWithLookup creates a new loader using the given lookup function. The prefix will be used to prefix the struct field names when they are used to read from environment variables.
func (*Loader) Load ¶
Load populates a struct with the values read returned by the specified lookup function. The struct must be specified as a pointer.
Load calls a lookup function for each public struct field. If the function returns a value, it is parsed according to the field type and assigned to the field.
Load uses the following rules to determine what name should be used to look up the value for a struct field:
- If the field has an "env" tag, use the tag value as the name, unless the tag is "-" in which case it means the field should be skipped.
- If the field has no "env" tag, turn the field name into UPPER_SNAKE_CASE format and use that as the name.
- Names are prefixed with the specified prefix.
The following types of struct fields are supported:
- types implementing Setter, TextUnmarshaler, BinaryUnmarshaler: the corresponding interface method will be used to populate the field with a string
- primary types (e.g. int, string): appropriate parsing functions will be called to parse a string value
- other types (e.g. array, struct): the string value is assumed to be in JSON format and is decoded/assigned to the field.
Special handling for nested structures:
- For fields that are structures (or pointers to structures), Load checks for a "prefix" tag. If found, this prefix is temporarily appended to the current prefix for loading nested fields, allowing hierarchical configuration management. The original prefix is restored afterwards.
- If a field is a nil pointer to a struct, it is automatically initialized to ensure that nested configurations can be loaded without prior manual initialization.
Load will log every field that is populated. In case when a field is tagged with `env:",secret"`, the value being logged will be masked for security purpose.
type LookupFunc ¶
LookupFunc looks up a name and returns the corresponding value and a flag indicating if the name is found.