dirconf/README.md -rw-r--r-- 2.1 KiB
009b06c4 — Philip K added reference to my article on dirconf 2 years ago


Dirconf implements Go library for a simple configuration format, that instead of storing everything in a file, uses the file system as a hierarchical key-value storage.

See my article for a more extensive discussion.


import dc "git.sr.ht/~zge/dirconf"

func main() {
	var name string
	var age int

	conf := dc.Conf{
		&dc.String{Ptr: &name, Must: true, Key: "name"},
		&dc.Int{Ptr: &age, Key: "person/age"},
		&dc.Bool{OnSet: someFunc, Key: "test/value"},

	err := conf.Parse()
	if err != nil {
		// handle error


The main two steps are:

  1. Listing all "Options" in a dirconf.Conf variable, which in turn is just a type-aliased slice of dirconf.Option objects. Such objects are dirconf.String, dirconf.Int, dirconf.Func, ...

  2. With all options listed, the Parse method (or ParseIn, if you want to manually specify the directory that should be used) will try to apply the file structure to the listed options.

    Parse will by default use ~/.config/[name of current binary] as the directory.

Most Options have a few properties in common, although the type system doesn't recognise this:

  • Ptr: A pointer to a variable that should be set to the parsed value. If nil (by default), it's ignored
  • Must: A Boolean value indicating that this value must value must have a corresponding file in the directory. If not set, an error will be returned from Parse (dirconf.ErrNoValueFound).
  • OnSet: Like Ptr, but will instead call a function with the value that has been found. Will be ignored if nil (by default).
  • Default: Value to use if nothing is found. Doesn't override Must. Not too useful for Ptr, more so for OnSet.
  • Key: A slash (/) delimited string that describes the local path the file for the value should be found in, within the configuration directory.

Dirconf is distributed under CC0. See LICENSE for more details