package module
Version: v0.0.0-...-70a46ee Latest Latest

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

Go to latest
Published: Dec 30, 2014 License: Apache-2.0 Imports: 11 Imported by: 1


ini Build Status

Package ini provides INI file read and write functionality in Go.



  • Load multiple data sources([]byte or file) 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.


go get

Getting Started

Loading from data sources

A Data Source is either raw data in type []byte or a file name with type string and you can load as many as data sources you want. Passing other types will simply return an error.

cfg, err := ini.Load([]byte("raw data"), "filename")

When you cannot decide how many data sources to load at the beginning, you still able to Append() them later.

err := cfg.Append("other file", []byte("other raw data"))
Working with sections

To get a section, you would need to:

section, err := cfg.GetSection("section name")

For a shortcut for default section, just give an empty string as name:

section, err := cfg.GetSection("")

When you're pretty sure the section exists, following code could make your life easier:

section := cfg.Section("")

What happens when the section somehow does not exists? Won't panic, it returns an empty section object.

To create a new section:

err := cfg.NewSection("new section")

To get a list of sections or section names:

sections := cfg.Sections()
names := cfg.SectionStrings()
Working with keys

To get a key under a section:

key, err := cfg.Section("").GetKey("key name")

Same rule applies to key operations:

key := cfg.Section("").Key("key name")

To create a new key:

err := cfg.Section("").NewKey("name", "value")

To get a list of keys or key names:

keys := cfg.Section().Keys()
names := cfg.Section().KeyStrings()

To get a clone hash of keys and corresponding values:

hash := cfg.GetSection("").KeysHash()
Working with values

To get a string value:

val := cfg.Section("").Key("key name").String()

To get value with types:

v, 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 = cfg.Section("").Key("BOOL").MustBool()
v = cfg.Section("").Key("FLOAT64").MustFloat64()
v = cfg.Section("").Key("INT").MustInt()
v = cfg.Section("").Key("INT64").MustInt64()

// 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.Seciont("").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)

What if my value is three-line long?

ADDRESS = """404 road,
NotFound, State, 5000

Not a problem!


/* --- start ---
404 road,
NotFound, State, 5000
------  end  --- */

That's all? Hmm, no.

Helper methods of working with values

To get value with given candidates:

v = cfg.Section("").Key("STRING").In("default", []string{"str", "arr", "types"})
v = cfg.Section("").Key("FLOAT64").InFloat64(1.1, []float64{1.25, 2.5, 3.75})
v = cfg.Section("").Key("INT").InInt(5, []int{10, 20, 30})
v = cfg.Section("").Key("INT64").InInt64(10, []int64{10, 20, 30})

Default value will be presented if value of key is not in candidates you given, and default value does not need be one of candidates.

To auto-split value into slice:

vals = cfg.Section("").Key("STRINGS").Strings(",")
vals = cfg.Section("").Key("FLOAT64S").Float64s(",")
vals = cfg.Section("").Key("INTS").Ints(",")
vals = cfg.Section("").Key("INT64S").Int64s(",")
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

NAME = Unknwon

cfg.Section("author").Key("GITHUB").String()		//
cfg.Section("package").Key("FULL_NAME").String()	//
Parent-child Sections

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


cfg.Section("package.sub").Key("CLONE_URL").String()	//
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.

-: Support read/write comments of keys and sections
-: Support auto-increment of key names
-: Support load multiple files to overwrite key values
cfg.Section("features").KeyStrings()	// []{"#1", "#2", "#3"}

Getting Help


What does BlockMode field do?

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 to version my package at this time.(PS: shorter import path)


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



Package ini provides INI file read and write functionality in Go.



View Source
const (


View Source
var (
	LineBreak = "\n"

	// Write spaces around "=" to look better.
	PrettyFormat = true


This section is empty.


type File

type File struct {
	// Should make things safe, but sometimes doesn't matter.
	BlockMode bool
	// contains filtered or unexported fields

File represents a combination of a or more INI file(s) in memory.

func Load

func Load(source interface{}, others ...interface{}) (_ *File, err error)

Load loads and parses from INI data sources. Arguments can be mixed of file name with string type, or raw data in []byte.

func (*File) Append

func (f *File) Append(source interface{}, others ...interface{}) error

Append appends one or more data sources and reloads automatically.

func (*File) DeleteSection

func (f *File) DeleteSection(name string)

DeleteSection deletes a section.

func (*File) GetSection

func (f *File) GetSection(name string) (*Section, error)

GetSection returns section by given name.

func (*File) NewSection

func (f *File) NewSection(name string) (*Section, error)

NewSection creates a new section.

func (*File) Reload

func (f *File) Reload() error

Reload reloads and parses all data sources.

func (*File) SaveTo

func (f *File) SaveTo(filename string) (err error)

SaveTo writes content to filesystem.

func (*File) Section

func (f *File) Section(name string) *Section

Section assumes named section exists and returns a zero-value when not.

func (*File) SectionStrings

func (f *File) SectionStrings() []string

SectionStrings returns list of section names.

func (*File) Sections

func (f *File) Sections() []*Section

Section returns list of Section.

type Key

type Key struct {
	Comment string
	// contains filtered or unexported fields

Key represents a key under a section.

func (*Key) Bool

func (k *Key) Bool() (bool, error)

Bool returns bool type value.

func (*Key) Float64

func (k *Key) Float64() (float64, error)

Float64 returns float64 type value.

func (*Key) Float64s

func (k *Key) Float64s(delim string) []float64

Float64s returns list of float64 devide by given delimiter.

func (*Key) In

func (k *Key) In(defaultVal string, candidates []string) string

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

func (*Key) InFloat64

func (k *Key) InFloat64(defaultVal float64, candidates []float64) float64

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

func (*Key) InInt

func (k *Key) InInt(defaultVal int, candidates []int) int

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

func (*Key) InInt64

func (k *Key) InInt64(defaultVal int64, candidates []int64) int64

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

func (*Key) Int

func (k *Key) Int() (int, error)

Int returns int type value.

func (*Key) Int64

func (k *Key) Int64() (int64, error)

Int64 returns int64 type value.

func (*Key) Int64s

func (k *Key) Int64s(delim string) []int64

Int64s returns list of int64 devide by given delimiter.

func (*Key) Ints

func (k *Key) Ints(delim string) []int

Ints returns list of int devide by given delimiter.

func (*Key) MustBool

func (k *Key) MustBool(defaultVal ...bool) bool

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

func (*Key) MustFloat64

func (k *Key) MustFloat64(defaultVal ...float64) float64

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

func (*Key) MustInt

func (k *Key) MustInt(defaultVal int

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

func (*Key) MustInt64

func (k *Key) MustInt64(defaultVal ...int64) int64

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

func (*Key) MustString

func (k *Key) MustString(defaultVal string) string

MustString returns default value if key value is empty.

func (*Key) Name

func (k *Key) Name() string

Name returns name of key.

func (*Key) SetValue

func (k *Key) SetValue(v string)

SetValue changes key value.

func (*Key) String

func (k *Key) String() string

String returns string representation of value.

func (*Key) Strings

func (k *Key) Strings(delim string) []string

Strings returns list of string devide by given delimiter.

func (*Key) Value

func (k *Key) Value() string

Value returns raw value of key for performance purpose.

type Section

type Section struct {
	Comment string
	// contains filtered or unexported fields

Section represents a config section.

func (*Section) DeleteKey

func (s *Section) DeleteKey(name string)

DeleteKey deletes a key from section.

func (*Section) GetKey

func (s *Section) GetKey(name string) (*Key, error)

GetKey returns key in section by given name.

func (*Section) Key

func (s *Section) Key(name string) *Key

Key assumes named Key exists in section and returns a zero-value when not.

func (*Section) KeyStrings

func (s *Section) KeyStrings() []string

KeyStrings returns list of key names of section.

func (*Section) Keys

func (s *Section) Keys() []*Key

Keys returns list of keys of section.

func (*Section) KeysHash

func (s *Section) KeysHash() map[string]string

KeysHash returns keys hash consisting of names and values.

func (*Section) Name

func (s *Section) Name() string

Name returns name of Section.

func (*Section) NewKey

func (s *Section) NewKey(name, val string) (*Key, error)

NewKey creates a new key to given section.

Source Files

Jump to

Keyboard shortcuts

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