Configuration library to load from multiple sources.
go get github.com/rakunlabs/chu
Define a struct to hold the configuration.
type Config struct {
Name string `cfg:"name"`
Age int `cfg:"age"`
}
And load the configuration.
cfg := Config{}
if err := chu.Load(ctx, "test", &cfg); err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
The configuration will be loaded from the following sources in order:
- Default
- File
- Environment
Check example folder to see how to use loaders with different kind of configuration.
Default loader is used to set default values from tag default
.
type Config struct {
Name string `cfg:"name" default:"John"`
Age int `cfg:"age" default:"30"`
}
Default supports numbers, string, bool, time.Duration and pointer of that types.
File loader is used to load configuration from file.
First checking CONFIG_PATH
env value and try current location to find in order of .toml
, .yaml
, .yml
, .json
extension with using given name.
Environment loader is used to load configuration from environment variables.
env
or cfg
tag can usable for environment loader.
export NAME=John
export AGE=30
type Config struct {
Name string `cfg:"name"`
Age int `cfg:"age"`
}