This package implements a basic configuration file parser language which provides a structure similar to what you would find on Microsoft Windows INI files.

The configuration file consists of sections, led by a "[section]" header and followed by "name: value" entries; "name=value" is also accepted. Note that leading whitespace is removed from values. The optional values can contain format strings which refer to other values in the same section, or values in a special DEFAULT section. Additional defaults can be provided on initialization and retrieval. Comments are indicated by ";" or "#"; a comment may begin anywhere on a line, including on the same line after parameters or section declarations.

For example:

[My Section]
foodir: %(dir)s/whatever

would resolve the "%(dir)s" to the value of "dir" (foo in this case). All reference expansions are done on demand.

The functionality and workflow is loosely based on the configparser package of the Python Standard Library.


$ goinstall



Operating instructions

Given a sample configuration file:

protocol: http://
base-url: %(protocol)s%(host)s

url: %(base-url)s/some/path
delegation: on
maxclients: 200 # do not set this higher
comments: This is a multi-line
    entry   # And this is a comment

To read this configuration file, do:

c, _ := config.ReadDefault("config.cfg")

c.String("service-1", "url")
// result is string ""

c.Int("service-1", "maxclients")
// result is int 200

c.Bool("service-1", "delegation")
// result is bool true

c.String("service-1", "comments")
// result is string "This is a multi-line\nentry"

Note the support for unfolding variables (such as %(base-url)s), which are read from the special (reserved) section name [DEFAULT].

A new configuration file can also be created with:

c := config.NewDefault()
c.AddOption("Section", "option", "value")
c.WriteFile("config.cfg", 0644, "A header for this file")

This results in the file:

# A header for this file

option: value

Note that sections, options and values are all case-sensitive.

Copyright 2009 The "goconfig" Authors
See file AUTHORS and CONTRIBUTORS (if any).

Licensed under Simplified BSD License.
See file LICENSE.


To contact, send e-mail through github.

Known bugs

To reporting bugs, please login and create a new issue here.

Credits and acknowledgments

For credits, see file AUTHORS and CONTRIBUTORS (if any).


For changes between releases, see file NEWS; and for changes between the commits, see the commit history.

Generated by gowizard