Documentation ¶
Overview ¶
Package config provides configuration values.
Configuration values are read from a file containing a JSON encoded map from keys to values. This file is searched in the current working directory, and recursively in its parent directories.
Example ¶
configMap := bytes.NewBufferString(`{ "Foo": 42, "Bar": {"Baz": 1} }`) var foo int var bar struct{ Baz int } Read(configMap) Value("Foo", &foo) Value("Bar", &bar) fmt.Println(foo, bar.Baz)
Output: 42 1
Index ¶
- func FindFileInParent(filename string, maxdepth int) (path string, err error)
- func Read(in io.Reader) (err error)
- func ReadFile(logger slog.Leveled, filename string, maxDepth int) (baseDir string, err error)
- func Value(key string, ret interface{}) (err error)
- func ValueOr(key string, ret interface{}, byDefault interface{}) (err error)
- type KeyNotFound
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FindFileInParent ¶
FindFileInParent search a file with the given filename. The search starts in the current directory then explores recursively the parent directories. The search fails after maxdepth changes of directory, i.e., when maxdepth is zero the file is search only in the current directory.
func Read ¶ added in v0.1.7
Read is a low-level function that reads the JSON configuration map directly from the given Reader.
func ReadFile ¶ added in v0.1.7
ReadFile reads a JSON file and stores the recorded values for subsequent calls to Value and ValueOr. This method must be called once before Value or ValueOr are called. When called multiple times, only the values from the last read file are available.
Types ¶
type KeyNotFound ¶
type KeyNotFound string
Error returned when the key is not found in the configuration.
func (KeyNotFound) Error ¶
func (self KeyNotFound) Error() string