Skip to content
forked from zieckey/goini

A simple, flexible, efficient and powerful INI parser for Golang.

License

Notifications You must be signed in to change notification settings

rl-devops/goini

 
 

Repository files navigation

goini

Build Status Coverage Status

This is a Go package to interact with arbitrary INI.

Our applications have a lot of memory data which are key/value pairs in the form of various separators NOT only \n. I have not found any Golang open source code suitable for it. So I write it myself.

goini is designed to be simple, flexible, efficient. It

  1. Supports the standard INI format
  2. Supports section
  3. Supports parsing INI file from local disk
  4. Supports parsing INI configuration data from memory
  5. Supports parsing data which are key/value pairs in the form of various separators NOT only \n
  6. Supports UTF8 encoding
  7. Supports comments which has a leading character ; or #
  8. Supports cascading inheritance
  9. Only depends standard Golang libraries
  10. Has 100% test coverage

Importing

import github.com/zieckey/goini

Usage

Example 1 : Parses an INI file

The simplest example code is :

import github.com/zieckey/goini

ini := goini.New()
err := ini.ParseFile(filename)
if err != nil {
	fmt.Printf("parse INI file %v failed : %v\n", filename, err.Error())
	return
}

v, ok := ini.Get("the-key")
//...

Example 2 : Parses the memory data with similar format of INI

raw := []byte("a:av||b:bv||c:cv||||d:dv||||||")
ini := goini.New()
err := ini.Parse(raw, "||", ":")
if err != nil {
    fmt.Printf("parse INI memory data failed : %v\n", err.Error())
    return
}

key := "a"
v, ok := ini.Get(key)
if ok {
    fmt.Printf("The value of %v is [%v]\n", key, v) // Output : The value of a is [av]
}

key = "c"
v, ok = ini.Get(key)
if ok {
    fmt.Printf("The value of %v is [%v]\n", key, v) // Output : The value of c is [cv]
}

Example 3 : Parses an inherited INI file

Assume we have a large project which has several production environments. Each production environment has its own configuration. But there is tiny difference between these configurations of the standalone production environments. So we use a common INI configuration to store the common configurations. And each production environment inherits from this common INI configuration.

The common.ini is bellow:

product=common
combo=common
debug=0

version=0.0.0.0
encoding=0

[sss]
a = aval
b = bval

The project1.ini is the configuration of project #1 as below:

inherited_from=common.ini

;the following config will override the values inherited from common.ini
product=project1
combo=test
debug=1

local=0
mid=c4ca4238a0b923820dcc509a6f75849b

[sss]
a = project1-aval
c = project1-cval

If we use goini.LoadInheritedINI("project1.ini") is the same as we have the following INI configuration:

product=project1
combo=test
debug=1

local=0
mid=c4ca4238a0b923820dcc509a6f75849b

version=0.0.0.0
encoding=0

[sss]
a = project1-aval
c = project1-cval

The value of the key product has been overwritten by value project1. The value of the key a in section sss has been overwritten by value project1-aval.

About

A simple, flexible, efficient and powerful INI parser for Golang.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%