Documentation
¶
Overview ¶
Example ¶
package main
import (
"fmt"
"github.com/jackc/envconf"
)
func main() {
config := envconf.New()
// Register configuration items.
config.Register(envconf.Item{
Name: "FOO",
Default: "default-foo",
Description: "the foo",
})
config.Register(envconf.Item{
Name: "BAR",
Default: "default-bar",
Description: "the bar",
})
// You can override the source of environment variables by setting LookupEnvFunc. Defaults to os.LookupEnv.
config.LookupEnvFunc = func(key string) (string, bool) {
if key == "FOO" {
return "foo from environment", true
}
return "", false
}
// You can use a config to create help output.
fmt.Println("Configuration items:")
for _, item := range config.Items() {
fmt.Printf("%s: %s (default: %s)\n", item.Name, item.Description, item.Default)
}
fmt.Println()
value := config.Value("FOO")
fmt.Println("FOO", value)
value = config.Value("BAR")
fmt.Println("BAR", value)
}
Output: Configuration items: BAR: the bar (default: default-bar) FOO: the foo (default: default-foo) FOO foo from environment BAR default-bar
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// LookupEnvFunc is used to fetch value of an environment variable.
LookupEnvFunc func(string) (string, bool)
// contains filtered or unexported fields
}
Config handles access to configuration stored in the environment.
func (*Config) Items ¶
Items returns all registered configuration items sorted by name alphabetically.
func (*Config) MustItem ¶
MustItem returns a configuration item by name. It panics if the item is not found.
Click to show internal directories.
Click to hide internal directories.