config

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

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

Go to latest
Published: Dec 6, 2021 License: MIT Imports: 7 Imported by: 0

README

config

GoDoc reference example

Extremely simple, yet efficient, file based configuration librairy

Features

  • lazily load configuration files for quick startup upon first read request (Get).
    • Set will always overwrite file configuration, even if lazy loading happens after Set was called.
  • get/set configuration keys
  • can save modified config on disk

Configuration file syntax

  • leading spaces and tabs ignored

  • blank lines are ignored

  • comment line start with # or //

  • key = value

    • key has no whitespace nor tabs, it is case sensitive
    • value starts at the first character following "=" until end of line. No quotes needed. Spaces surrounding the = sign are removed.
  • [ mainkey ] defines a section, where all following keys, until another section is defined, are prefixed with mainkey.

  • [] defines a section witjhout prefix. This is the default.

Example configuration

    // this is a comment
    version = 3.2.1
    # this is also a comment

    [ date ]
    created = 1/8/2019
    modified = 3/8/2019

    // above will give : config.Get("date.created") --> "1/8/2019"

See examples

Typical use

    import ".../config"

    conf := config.New("file1.conf", "file2.conf","/usr/bin/file3.conf", "~/bin/file4.conf")

    // save config data, including modified values.
    defer conf.Save("myconf.conf")

    // access data 
    sn := conf.Get("surname") == "" 

    // set data
    conf.Set("name","John Doe")

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultFiles = []string{"config.conf", "~/config.conf", "../config.conf", "/config.conf"}

DefaultFiles searched after the ones specified in New, only if needed. Failure to find a valid config file will NOT generate an error.

Functions

This section is empty.

Types

type Config

type Config interface {
	Get(key string) string
	Set(key, value string)
	// Save to the provided file name, including all Set operations done until now.
	Save(fileName string) error
}

Config is the main interface to read or change the configuration keys. All keys are string. Default key value is empty string.

func New

func New(files ...string) Config

New creates a new Config. Provided file names will be searched, in order, then the DefaultFiles, until a valid file is found. If none is found, all keys have an empty string value.

Jump to

Keyboard shortcuts

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