Skip to content

Read INI

The core reads and writes only YAML. INI comes from a sibling module, config-ini, which adds nothing to your dependency graph — an INI file is read with the standard library.

go get gitlab.com/phpboyscout/go/config-ini
import (
    "gitlab.com/phpboyscout/go/config"
    configini "gitlab.com/phpboyscout/go/config-ini"
)

store, err := config.NewStore(ctx,
    config.WithFiles(fsys, "/etc/app.yaml"),                  // YAML, the default
    config.WithBackend(configini.New(fsys, "/etc/app.ini")),  // outranks it
)

Sections and keys nest on dots

A [section] header prefixes the keys beneath it:

[server]
host = localhost
port = 8080
host := store.View().GetString("server.host") // "localhost"
port := store.View().GetInt("server.port")    // 8080

An INI layer therefore merges into a nested YAML or JSON one by the same paths. A section name or a key may itself contain dots, each a nesting separator — [db.primary] with pool.size = 4 reads as db.primary.pool.size. Keys before any section are top-level. A line is key = value or key : value, split on whichever separator comes first so a colon in a value (a URL) is not mis-split; ; and # begin a comment; and surrounding quotes are stripped.

Read-only

Values are strings; the read path casts. The format is read-only.

What it costs

Only the config graph — an INI file is parsed with the standard library, so there is no parser module. An allowlist test in the module states it. No filesystem library: you supply the config.FS.