goconfig

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: May 25, 2016 License: Apache-2.0, Apache-2.0 Imports: 13 Imported by: 0

README

goconfig Build Status Go Walker

中文文档

IMPORTANT

  • This library is under bug fix only mode, which means no more features will be added.
  • I'm continuing working on better Go code with a different library: ini.

About

Package goconfig is a easy-use, comments-support configuration file parser for the Go Programming 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 or name=value entries. 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. Comments are indicated by ";" or "#"; comments may begin anywhere on a single line.

Features

  • It simplified operation processes, easy to use and undersatnd; therefore, there are less chances to have errors.
  • It uses exactly the same way to access a configuration file as you use Windows APIs, so you don't need to change your code style.
  • It supports read recursion sections.
  • It supports auto increment of key.
  • It supports READ and WRITE configuration file with comments each section or key which all the other parsers don't support!!!!!!!
  • It supports get value through type bool, float64, int, int64 and string, methods that start with "Must" means ignore errors and get zero-value if error occurs, or you can specify a default value.
  • It's able to load multiple files to overwrite key values.

Installation

go get github.com/Unknwon/goconfig

Or

gopm get github.com/Unknwon/goconfig

API Documentation

Go Walker.

Example

Please see conf.ini as an example.

Usage
  • Function LoadConfigFile load file(s) depends on your situation, and return a variable with type ConfigFile.
  • GetValue gives basic functionality of getting a value of given section and key.
  • Methods like Bool, Int, Int64 return corresponding type of values.
  • Methods start with Must return corresponding type of values and returns zero-value of given type if something goes wrong.
  • SetValue sets value to given section and key, and inserts somewhere if it does not exist.
  • DeleteKey deletes by given section and key.
  • Finally, SaveConfigFile saves your configuration to local file system.
  • Use method Reload in case someone else modified your file(s).
  • Methods contains Comment help you manipulate comments.

More Information

  • All characters are CASE SENSITIVE, BE CAREFUL!

Credits

License

This project is under Apache v2 License. See the LICENSE file for the full license text.

Documentation

Overview

Package goconfig is a fully functional and comments-support configuration file(.ini) parser.

Index

Constants

View Source
const (
	// Default section name.
	DEFAULT_SECTION = "DEFAULT"
)

Variables

View Source
var LineBreak = "\n"
View Source
var PrettyFormat = true

Write spaces around "=" to look better.

Functions

func SaveConfigFile

func SaveConfigFile(c *ConfigFile, filename string) (err error)

SaveConfigFile writes configuration file to local file system

Types

type ConfigFile

type ConfigFile struct {
	BlockMode bool // Indicates whether use lock or not.
	// contains filtered or unexported fields
}

A ConfigFile represents a INI formar configuration file.

func LoadConfigFile

func LoadConfigFile(fileName string, moreFiles ...string) (c *ConfigFile, err error)

LoadConfigFile reads a file and returns a new configuration representation. This representation can be queried with GetValue.

func LoadFromData

func LoadFromData(data []byte) (c *ConfigFile, err error)

LoadFromData accepts raw data directly from memory and returns a new configuration representation.

func (*ConfigFile) AppendFiles

func (c *ConfigFile) AppendFiles(files ...string) error

AppendFiles appends more files to ConfigFile and reload automatically.

func (*ConfigFile) Bool

func (c *ConfigFile) Bool(section, key string) (bool, error)

Bool returns bool type value.

func (*ConfigFile) DeleteKey

func (c *ConfigFile) DeleteKey(section, key string) bool

DeleteKey deletes the key in given section. It returns true if the key was deleted, or returns false if the section or key didn't exist.

func (*ConfigFile) DeleteSection

func (c *ConfigFile) DeleteSection(section string) bool

DeleteSection deletes the entire section by given name. It returns true if the section was deleted, and false if the section didn't exist.

func (*ConfigFile) Float64

func (c *ConfigFile) Float64(section, key string) (float64, error)

Float64 returns float64 type value.

func (*ConfigFile) GetKeyComments

func (c *ConfigFile) GetKeyComments(section, key string) (comments string)

GetKeyComments returns the comments of key in the given section. It returns an empty string(0 length) if the comments do not exist.

func (*ConfigFile) GetKeyList

func (c *ConfigFile) GetKeyList(section string) []string

GetKeyList returns the list of all keys in give section in the same order in the file. It returns nil if given section does not exist.

func (*ConfigFile) GetSection

func (c *ConfigFile) GetSection(section string) (map[string]string, error)

GetSection returns key-value pairs in given section. It section does not exist, returns nil and error.

func (*ConfigFile) GetSectionComments

func (c *ConfigFile) GetSectionComments(section string) (comments string)

GetSectionComments returns the comments in the given section. It returns an empty string(0 length) if the comments do not exist.

func (*ConfigFile) GetSectionList

func (c *ConfigFile) GetSectionList() []string

GetSectionList returns the list of all sections in the same order in the file.

func (*ConfigFile) GetValue

func (c *ConfigFile) GetValue(section, key string) (string, error)

GetValue returns the value of key available in the given section. If the value needs to be unfolded (see e.g. %(google)s example in the GoConfig_test.go), then String does this unfolding automatically, up to _DEPTH_VALUES number of iterations. It returns an error and empty string value if the section does not exist, or key does not exist in DEFAULT and current sections.

func (*ConfigFile) Int

func (c *ConfigFile) Int(section, key string) (int, error)

Int returns int type value.

func (*ConfigFile) Int64

func (c *ConfigFile) Int64(section, key string) (int64, error)

Int64 returns int64 type value.

func (*ConfigFile) MustBool

func (c *ConfigFile) MustBool(section, key string, defaultVal ...bool) bool

MustBool always returns value without error, it returns false if error occurs.

func (*ConfigFile) MustFloat64

func (c *ConfigFile) MustFloat64(section, key string, defaultVal ...float64) float64

MustFloat64 always returns value without error, it returns 0.0 if error occurs.

func (*ConfigFile) MustInt

func (c *ConfigFile) MustInt(section, key string, defaultVal ...int) int

MustInt always returns value without error, it returns 0 if error occurs.

func (*ConfigFile) MustInt64

func (c *ConfigFile) MustInt64(section, key string, defaultVal ...int64) int64

MustInt64 always returns value without error, it returns 0 if error occurs.

func (*ConfigFile) MustValue

func (c *ConfigFile) MustValue(section, key string, defaultVal ...string) string

MustValue always returns value without error. It returns empty string if error occurs, or the default value if given.

func (*ConfigFile) MustValueArray

func (c *ConfigFile) MustValueArray(section, key, delim string) []string

MustValueArray always returns value array without error, it returns empty array if error occurs, split by delimiter otherwise.

func (*ConfigFile) MustValueRange

func (c *ConfigFile) MustValueRange(section, key, defaultVal string, candidates []string) string

MustValueRange always returns value without error, it returns default value if error occurs or doesn't fit into range.

func (*ConfigFile) MustValueSet

func (c *ConfigFile) MustValueSet(section, key string, defaultVal ...string) (string, bool)

MustValue always returns value without error, It returns empty string if error occurs, or the default value if given, and a bool value indicates whether default value is returned.

func (*ConfigFile) Reload

func (c *ConfigFile) Reload() (err error)

Reload reloads configuration file in case it has changes.

func (*ConfigFile) SetKeyComments

func (c *ConfigFile) SetKeyComments(section, key, comments string) bool

SetKeyComments adds new section-key comments to the configuration. If comments are empty(0 length), it will remove its section-key comments! It returns true if the comments were inserted or removed, or returns false if the comments were overwritten. If the section does not exist in advance, it is created.

func (*ConfigFile) SetSectionComments

func (c *ConfigFile) SetSectionComments(section, comments string) bool

SetSectionComments adds new section comments to the configuration. If comments are empty(0 length), it will remove its section comments! It returns true if the comments were inserted or removed, or returns false if the comments were overwritten.

func (*ConfigFile) SetValue

func (c *ConfigFile) SetValue(section, key, value string) bool

SetValue adds a new section-key-value to the configuration. It returns true if the key and value were inserted, or returns false if the value was overwritten. If the section does not exist in advance, it will be created.

type ParseError

type ParseError int
const (
	ERR_SECTION_NOT_FOUND ParseError = iota + 1
	ERR_KEY_NOT_FOUND
	ERR_BLANK_SECTION_NAME
	ERR_COULD_NOT_PARSE
)

Jump to

Keyboard shortcuts

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