You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Load multiple data sources([]byte, file and io.ReadCloser) with overwrites.
Read with recursion values.
Read with parent-child sections.
Read with auto-increment key names.
Read with multiple-line values.
Read with tons of helper methods.
Read and convert values to Go types.
Read and WRITE comments of sections and keys.
Manipulate sections, keys and comments with ease.
Keep sections and keys in order as you parse and save.
Installation
To use a tagged revision:
go get gopkg.in/ini.v1
To use with latest changes:
go get github.com/go-ini/ini
Please add -u flag to update in the future.
Testing
If you want to test on your machine, please apply -t flag:
go get -t gopkg.in/ini.v1
Please add -u flag to update in the future.
Getting Started
Loading from data sources
A Data Source is either raw data in type []byte, a file name with type string or io.ReadCloser. You can load as many data sources as you want. Passing other types will simply return an error.
cfg, err:=ini.Load([]byte("raw data"), "filename", ioutil.NopCloser(bytes.NewReader([]byte("some other data"))))
Or start with an empty object:
cfg:=ini.Empty()
When you cannot decide how many data sources to load at the beginning, you will still be able to Append() them later.
err:=cfg.Append("other file", []byte("other raw data"))
If you have a list of files with possibilities that some of them may not available at the time, and you don't know exactly which ones, you can use LooseLoad to ignore nonexistent files without returning error.
The cool thing is, whenever the file is available to load while you're calling Reload method, it will be counted as usual.
Ignore cases of key name
When you do not care about cases of section and key names, you can use InsensitiveLoad to force all names to be lowercased while parsing.
cfg, err:=ini.InsensitiveLoad("filename")
//...// sec1 and sec2 are the exactly same section objectsec1, err:=cfg.GetSection("Section")
sec2, err:=cfg.GetSection("SecTIOn")
// key1 and key2 are the exactly same key objectkey1, err:=sec1.GetKey("Key")
key2, err:=sec2.GetKey("KeY")
MySQL-like boolean key
MySQL's configuration allows a key without value as follows:
[mysqld]
...
skip-host-cache
skip-name-resolve
By default, this is considered as missing value. But if you know you're going to deal with those cases, you can assign advanced load options:
If you do not want any auto-transformation (such as recursive read) for the values, you can get raw value directly (this way you get much better performance):
val:=cfg.Section("").Key("key name").Value()
To check if raw value exists:
yes:=cfg.Section("").HasValue("test value")
To get value with types:
// For boolean values:// true when value is: 1, t, T, TRUE, true, True, YES, yes, Yes, y, ON, on, On// false when value is: 0, f, F, FALSE, false, False, NO, no, No, n, OFF, off, Offv, err=cfg.Section("").Key("BOOL").Bool()
v, err=cfg.Section("").Key("FLOAT64").Float64()
v, err=cfg.Section("").Key("INT").Int()
v, err=cfg.Section("").Key("INT64").Int64()
v, err=cfg.Section("").Key("UINT").Uint()
v, err=cfg.Section("").Key("UINT64").Uint64()
v, err=cfg.Section("").Key("TIME").TimeFormat(time.RFC3339)
v, err=cfg.Section("").Key("TIME").Time() // RFC3339v=cfg.Section("").Key("BOOL").MustBool()
v=cfg.Section("").Key("FLOAT64").MustFloat64()
v=cfg.Section("").Key("INT").MustInt()
v=cfg.Section("").Key("INT64").MustInt64()
v=cfg.Section("").Key("UINT").MustUint()
v=cfg.Section("").Key("UINT64").MustUint64()
v=cfg.Section("").Key("TIME").MustTimeFormat(time.RFC3339)
v=cfg.Section("").Key("TIME").MustTime() // RFC3339// Methods start with Must also accept one argument for default value// when key not found or fail to parse value to given type.// Except method MustString, which you have to pass a default value.v=cfg.Section("").Key("String").MustString("default")
v=cfg.Section("").Key("BOOL").MustBool(true)
v=cfg.Section("").Key("FLOAT64").MustFloat64(1.25)
v=cfg.Section("").Key("INT").MustInt(10)
v=cfg.Section("").Key("INT64").MustInt64(99)
v=cfg.Section("").Key("UINT").MustUint(3)
v=cfg.Section("").Key("UINT64").MustUint64(6)
v=cfg.Section("").Key("TIME").MustTimeFormat(time.RFC3339, time.Now())
v=cfg.Section("").Key("TIME").MustTime(time.Now()) // RFC3339
How do you transform this to regular format automatically?
cfg, err:=ini.LoadSources(ini.LoadOptions{UnescapeValueDoubleQuotes: true}, "en-US.ini"))
cfg.Section("<name of your section>").Key("create_repo").String()
// You got: created repository <a href="%s">%s</a>
By default, spaces are used to align "=" sign between key and values, to disable that:
ini.PrettyFormat=false
Advanced Usage
Recursive Values
For all value of keys, there is a special syntax %(<name>)s, where <name> is the key name in same section or default section, and %(<name>)s will be replaced by corresponding value(empty string if key not found). You can use this syntax at most 99 level of recursions.
NAME = ini
[author]NAME = Unknwon
GITHUB = https://github.com/%(NAME)s
[package]FULL_NAME = github.com/go-ini/%(NAME)s
You can use . in section name to indicate parent-child relationship between two or more sections. If the key not found in the child section, library will try again on its parent section until there is no parent section.
NAME = ini
VERSION = v1
IMPORT_PATH = gopkg.in/%(NAME)s.%(VERSION)s
[package]CLONE_URL = https://%(IMPORT_PATH)s
[package.sub]
Sometimes, you have sections that do not contain key-value pairs but raw content, to handle such case, you can use LoadOptions.UnparsableSections:
cfg, err:=ini.LoadSources(ini.LoadOptions{UnparseableSections: []string{"COMMENTS"}}, `[COMMENTS]<1><L.Slide#2> This slide has the fuel listed in the wrong units <e.1>`))
body:=cfg.Section("COMMENTS").Body()
/* --- start ---<1><L.Slide#2> This slide has the fuel listed in the wrong units <e.1>------ end --- */
Auto-increment Key Names
If key name is - in data source, then it would be seen as special syntax for auto-increment key name start from 1, and every section is independent on counter.
[features]
-: Support read/write comments of keys and sections
-: Support auto-increment of key names
-: Support load multiple files to overwrite key values
By default, library lets you read and write values so we need a locker to make sure your data is safe. But in cases that you are very sure about only reading data through the library, you can set cfg.BlockMode = false to speed up read operations about 50-70% faster.
Why another INI library?
Many people are using my another INI library goconfig, so the reason for this one is I would like to make more Go style code. Also when you set cfg.BlockMode = false, this one is about 10-30% faster.
To make those changes I have to confirm API broken, so it's safer to keep it in another place and start using gopkg.in to version my package at this time.(PS: shorter import path)
License
This project is under Apache v2 License. See the LICENSE file for the full license text.
About
Package ini provides INI file read and write functionality in Go.