config

package module
v0.0.0-...-a8b9499 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2021 License: MIT Imports: 5 Imported by: 1

README

Config

Build Status

Configuration file parser for Go. For documentation and examples check here: http://godoc.org/github.com/pilu/config

Author

Andrea Franz - http://gravityblast.com

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Documentation

Overview

Package config parses files with format similar to INI files. Comments starts with '#' or ';'. Each line define a key and a value, both strings. Between them you can use =, : or just spaces.

foo: bar
foo = bar
foo bar

In the above example the key is 'key' and the value is 'value with spaces'. You can also specify sections:

[section_1]
foo 1

[section_2]
foo 2

All top level options are grouped in a main section. The main section name is passed to the ParseFile function.

sections, err := config.ParseFile("test.conf", mainSectionName)

ParseFile returns a map where keys are section names, and values are options. Options are simple map with string for both keys and values.

Example file:

# comment 1
# comment 2

url http://example.com

[development]
db.host     localhost
db.username foo-dev
db.password bar-dev

[production]
db.host     example.com
db.username foo-production
db.password bar-production

Usage example:

package main

import (
  "fmt"
  "github.com/pilu/config"
)

func main() {
  mainSectionName := "main"
  // Top level options are grouped in a section called "main"
  sections, err := config.ParseFile("test.conf", mainSectionName)
  if err != nil {
    panic(err)
  }

  for section, options := range sections {
    fmt.Printf("'%s': \n", section)
    for key, value := range options {
      fmt.Printf("  '%s' = '%s' \n", key, value)
    }
  }

}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options map[string]string

type Sections

type Sections map[string]Options

func ParseFile

func ParseFile(path string, mainSectionName string) (Sections, error)

Jump to

Keyboard shortcuts

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