Documentation
¶
Overview ¶
Package goconfig provides a flexible and powerful configuration utility for Go applications. It allows loading environment variables into structs with support for various data types and customization options.
Key features:
- Load environment variables into struct fields automatically
- Support for nested structs and pointers
- Customizable field name transformation
- Support for various data types (string, bool, int, uint, float, duration, slices, maps)
- Configurable prefix and separators
- Tag-based field mapping with env and alias tags
- Anonymous struct embedding support
- Panic recovery for safe error handling
Example usage:
type Config struct { Host string `env:"HOST"` Port int Timeout time.Duration Debug bool Numbers []int Settings map[string]string } var cfg Config if err := goconfig.Load(&cfg); err != nil { log.Fatal(err) }
Index ¶
Constants ¶
const ( // DefaultSep is the default separator used for environment variable names DefaultSep string = "_" // DefaultArrSep is the default separator used for array values DefaultArrSep string = "," )
Variables ¶
This section is empty.
Functions ¶
func Load ¶
Load loads configuration from environment variables into the provided struct. It uses default options and is a convenience wrapper around New().Load(). The struct should be a pointer to a struct with fields tagged with "env" or "alias" tags.
func UperCaseTransformer ¶
UperCaseTransformer transforms PascalCase field names into MACRO_CASE environment variable names. It handles various cases including:
- DBConnection => DB_CONNECTION
- AKey => A_KEY
- KeyA => KEY_A
- ThisISMyKey => THIS_IS_MY_KEY
This transformer is useful when you want to maintain a consistent uppercase naming convention for environment variables while using PascalCase in your Go code.
func VoidTransformer ¶
VoidTransformer is the default field name transformer that returns the key unchanged. It can be used with WithKeyTransformer to maintain the original field names.
Types ¶
type Loader ¶
type Loader struct {
// contains filtered or unexported fields
}
Loader handles the loading of configuration from environment variables into structs. It provides customization options for prefix, separators, and field name transformation.
type Option ¶
type Option func(*Loader)
Option is a function type that modifies a Loader's configuration.
func WithArraySeparator ¶
WithArraySeparator sets the separator used for array values in environment variables. The default separator is ",". For example, with separator ";" and array field "Numbers", the environment variable would be "1;2;3;4".
func WithKeyTransformer ¶
WithKeyTransformer sets a custom function to transform field names into environment variable names. This allows for custom naming conventions beyond the default behavior. The transformer function receives the field name and returns the transformed name.
func WithPrefix ¶
WithPrefix sets a prefix for all environment variables. The name of the environment variable will be prefix_fieldName. For example, with prefix "APP" and field "Host", the environment variable would be "APP_HOST".
func WithSeparator ¶
WithSeparator sets the separator used for nested field names in environment variables. The default separator is "_". For example, with separator "." and nested field "DB.Host", the environment variable would be "DB.HOST".
Directories
¶
Path | Synopsis |
---|---|
Package configtype provides a flexible and type-safe way to load and manage configuration from various sources.
|
Package configtype provides a flexible and type-safe way to load and manage configuration from various sources. |