Documentation ¶
Overview ¶
Package config provides a way to to supply runtime configuration values, loosely coupled. You can use config to read the environment, or to provide an arbitrarily complex configuration mechanism, without binding your application tightly to that mechanism.
As provided, config.Default() will return a Getter that provides access to values from the environment. Implement your own Loader to mutate the environment before other packages consume configuration values, or implement a custom Getter to use some other configuration scheme.
Several packages under efixler are dependent on the config package.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetLoader ¶
func SetLoader(cl Loader)
SetLoader : Pass a Loader that will be utilized to supply the Getter returned by Default(). The Loader function will be called upon the first call to Default() after SetLoader(). Each call to SetLoader will clear the current default Getter.
A Loader can write values into the environment and return an Enviroment() Getter, or it can return anything that implements the Getter interface. The example shows the former case.
If you never call SetLoader() (or if you pass a nil value), the Default() Getter will pass through environment variables.
SetLoader() should be called early, preferably in an init() method as close as possible to the application's entry point, to ensure that consumers get the right configuration as they are initializing.
Example ¶
f := func(ctx context.Context) Getter { os.Setenv("mySpecialConfig", "happy") return Environment() } SetLoader(f) config := Default() fmt.Println(config.Get("mySpecialConfig"))
Output: happy
Types ¶
type Env ¶
type Env struct { }
Env is a Getter implementation that reads from the environment.
func (*Env) Get ¶
Get : Equivalent to os.Getenv(key). Note that other Get-ish methods in Env call Env.Get() (and not os.Getenv)
func (*Env) GetOrDefault ¶
GetOrDefault : If the requested key is not present or empty, return the dflt.
func (*Env) GetStrings ¶
GetStrings will treat a comma-delimited config value as an []string, stripping whitespace around the commas.
type Getter ¶
type Getter interface { Get(string) string GetOrDefault(string, string) string GetStrings(string) []string MustGet(string) string }
Getter : Core interface for implementations providing configuration data to consumers.
type Loader ¶
Loader is a callback function that used to delegate configuration loading. Loader is expected to return a Getter that will be used by consumers to access configuration values. If the Loader copies configuration values into the environment, it can return a standard environment getter using Environment(). See the SetLoader() example.
The Context argument is defined to provide a hook for per-request mutated configs (which is not yet implemented). Calls to the Loader function can expect to receive a nil Context.