Your friendly .ini config file parser, written in Go.
package main
import ("fmt"; "github.com/jotaen/genie")
const iniText = `
# Comment.
key = value
[section1]
foo = bar
test = 123
`
func main() {
data, err := genie.Parse(iniText)
if err != nil {
panic(err)
}
// Access values directly, either from top-level or from section:
fmt.Println(data.Get("key")) // Prints `value`.
fmt.Println(data.GetFromSection("section1", "foo")) // Prints `bar`.
fmt.Println(data.GetFromSection("section1", "test")) // Prints `123`.
// Retrieve all values from a section:
section1 := data.GetSection("section1")
fmt.Println(section1.Get("foo")) // Prints `bar`.
fmt.Println(section1.Get("test")) // Prints `123`.
// Accessing non-existing values returns empty string:
fmt.Println(data.Get("asdfasdf")) // Prints empty string.
fmt.Println(data.GetFromSection("section26487", "bla")) // Prints empty string.
}- An entry is a key/value pair.
- The delimiter between key and value is the sequence of a
=character surrounded by a spacecharacter on each side. - The key itself cannot contain whitespace.
- All what follows behind the delimiter is value-land, including any whitespace,
#’s, or really whatever. - The value might be absent, in which case the space behind the
=may be absent too. - There is no difference between “absent” value or “empty” value.
- Both the key and the value are case-sensitive.
- A label wrapped in square brackets denotes the section for all following entries (until the next section).
- All entries until the first explicit section belong to the top-level section.
- Section names cannot be empty or blank; they are case-sensitive.
- If a line starts with
#, it’s treated as comment and is ignored. - There generally can’t be trailing comments.
- Whitespace is a space
or a tab\t. - Lines can never start with whitespace, unless they are all blank.