package module
v0.0.0-...-cf9b1b2 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2019 License: ISC Imports: 2 Imported by: 0



Config is a small, low-level package for reading configuration. The syntax if very simple and flexible, it resembles the syntax of httpd's configuration file syntax.

The package does no IO and does not load the configuration into a map or struct. Instead a callback must be provided, which is called for every entry in the given configuration.

See example_test.go for a complete example.

NOTICE: Config is useful, but still in development. There might be bug here and there. :)


go get -u





This section is empty.


This section is empty.


func Parse

func Parse(input string, callback CommandFn) error

Parse reads the input and calls callback whenever it finishes reading a command.

package main

import (


type configEntry struct {
	Key    string
	Values []string

func main() {
	input := `
prefork 2

server "default" {
	listen on * port 80

types {
	text/css		css
	text/html		html htm
	text/plain		txt
	image/gif		gif
	image/jpeg		jpeg jpg
	image/png		png
	application/javascript	js
	application/xml		xml

server "" {
	alias ""
	listen on * port 80
	listen on * tls port 443
	root "/htdocs/"

server "" {
	listen on port 80
	root "/htdocs/"

	entries := []configEntry{}

	err := config.Parse(input, func(namespace, params []string) {
		key := strings.Join(namespace, ".")
		val := make([]string, len(params))
		copy(val, params)

		entries = append(entries, configEntry{
			Key:    key,
			Values: val,

	if err != nil {

	padding := maxKeyLen(entries) + 4

	for _, e := range entries {
		padding := strings.Repeat(" ", padding-len(e.Key))
		fmt.Printf("%s%s%q\n", e.Key, padding, e.Values)


func maxKeyLen(entries []configEntry) int {
	max := 0

	for _, e := range entries {
		keyLen := len(e.Key)
		if keyLen > max {
			max = keyLen

	return max

prefork                         ["2"]
server                          ["\"default\""]
server.listen                   ["on" "*" "port" "80"]
types                           []
types.text/css                  ["css"]
types.text/html                 ["html" "htm"]
types.text/plain                ["txt"]
types.image/gif                 ["gif"]
types.image/jpeg                ["jpeg" "jpg"]
types.image/png                 ["png"]
types.application/javascript    ["js"]
types.application/xml           ["xml"]
server                          ["\"\""]
server.alias                    ["\"\""]
server.listen                   ["on" "*" "port" "80"]
server.listen                   ["on" "*" "tls" "port" "443"]
server.root                     ["\"/htdocs/\""]
server                          ["\"\""]
server.listen                   ["on" "" "port" "80"]
server.root                     ["\"/htdocs/\""]


type CommandFn

type CommandFn func([]string, []string)

CommandFn is called whenever the parser finished reading a command.

It's arguments are the current namespace and parameters (excluding blocks) as a string slice.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL