config

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2019 License: MIT Imports: 5 Imported by: 0

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