56 lines
1.9 KiB
Markdown
56 lines
1.9 KiB
Markdown
# package conf
|
|
|
|
**go get code.uint32.ru/tiny/conf** to download.
|
|
|
|
**import "code.uint32.ru/tiny/conf"** to use in your code.
|
|
conf implements a very simple config parser with permissive input syntax.
|
|
|
|
It parses input file or io.Reader looking for key value pairs separated by equal
|
|
sign into conf.Map which is a map[string]conf.Value under the hood.
|
|
|
|
Input syntax rules are as follows:
|
|
|
|
1. Lines starting with "#" are considered to be comments and are discarded. If a line starts with any number of whitespace characters followed by "#" it is also discarded as comment.
|
|
|
|
2. If a line is not a comment it must follow the "key = value" pattern. Any of the following is valid: "key=value", "key= value", "key =value". Leading and trailing whitespace characters are trimmed before parsing line. Whitespaces surrounding
|
|
key and value strings are also trimmed.
|
|
|
|
3. Keys in the input must be unique, ohterwise Read / ReadFile methods will return an error.
|
|
|
|
Map object has Find, Get and GetDefault methods used to retrieve Value from Map by corresponding key.
|
|
|
|
Value is essentially a string which can be converted into several types using Value's methods:
|
|
|
|
**String()** returns Value as string
|
|
|
|
**StringSlice()** interprets Value as a comma-separated list (whitespace around elements is trimmed)
|
|
|
|
**Int()** converts Value to int
|
|
|
|
**IntSlice()** tries to convert Value to []int
|
|
|
|
**Float64()** converts Value to float64
|
|
|
|
**Float64Slice()** tries to convert to []float64
|
|
|
|
**Map()** tries to interpret Value as comma-separated list of key-value pairs like in "key1: value1, key2: value2, key3: value3"
|
|
|
|
**URL()** calls url.Parse to convert Value to *url.URL
|
|
|
|
See documentation for description of all available methods.
|
|
|
|
Example usage:
|
|
```go
|
|
m, _ := conf.ReadFile("myfile")
|
|
retries, err := m.Get("retries").Int()
|
|
if err != nil {
|
|
// handle err
|
|
}
|
|
|
|
addr, err := m.Get("addr").URL()
|
|
if err != nil {
|
|
// handle err
|
|
}
|
|
```
|
|
|
|
See example folder. |