Awesome Go

ini

CategoryConfiguration
SubcategoryConfiguration
Stars3,538

Go package to read and write INI files

About ini

INI

GoDoc

Package ini reads and writes INI files in Go.

Features

  • Fast loading: a single-pass lexer, linear in the size of the input and up to 37× faster than v1.
  • Several sources: files, []byte and io.Reader, with later sources overriding earlier ones.
  • Rich INI syntax: parent-child sections, auto-increment keys, repeated (shadow) keys, nested and multiline values, and %(name)s references.
  • Typed values: numbers, booleans, durations and times, with generic lists and candidates of any of them, and ranges of all but booleans.
  • Struct mapping: fill a struct from a file, or a file from a struct, guided by ini struct tags. Types can format and parse their own values with MarshalINI and UnmarshalINI.
  • Safe writes: comments and order are kept, and what is written loads back the same, or Write returns an error.
  • Precise errors: parse errors give the line and column.
  • Concurrency: safe for concurrent use, or lock-free with Locking(false) for files that never change.

Installation

The minimum requirement of Go is 1.26.

go get unknwon.dev/ini/v2@latest

Getting started

package main

import (
	"fmt"
	"log"
	"time"

	"unknwon.dev/ini/v2"
)

func main() {
	f, err := ini.Load("app.ini")
	if err != nil {
		log.Fatal(err) // e.g., "line 3, column 1: key-value delimiter not found: broken line"
	}

	server := f.Section("server")
	port := server.Key("port").MustInt(8080)
	timeout := server.Key("timeout").MustDuration(30 * time.Second)
	fmt.Println(port, timeout)

	server.Key("port").SetValue("9090")
	if err := f.SaveTo("app.ini"); err != nil {
		log.Fatal(err)
	}
}

Options change the syntax that is accepted, such as ini.Load("app.ini", ini.AllowShadows(true), ini.IgnoreInlineComment(true)). The package documentation has examples of struct mapping, typed lists and write options.

Performance

v2 loads files 3 to 37 times faster than v1, with a seventh to a two-hundredth of the allocations:

Operationv1.67.3v2Allocations, v1 → v2
Load 10,000 keys in one section77 ms2.1 ms (37× faster)40.2k → 0.2k
Load 10,000 keys in 1,000 sections6.6 ms2.2 ms (3.0× faster)55.1k → 8.1k
Load 10,000 keys, mixed syntax6.7 ms2.3 ms (2.9× faster)54.8k → 8.4k
Write 10,000 keys1.7–2.7 ms3.1–4.1 ms (1.5–1.8× slower)17–8.9k → 10.0k–22.8k
MapTo a 13-field struct2.7 µs4.7 µs50 → 48
ReflectFrom the same struct2.8 µs4.8 µs51 → 48
Read one key44 ns58 ns, or 32 ns with Locking(false)0 → 0
  • Load is linear in the number of keys. v1 compared each new key with every key before it in its section, so a large section took quadratic time.
  • Write is slower because it checks that every line it writes reads back as the same value. v1 could write files that loaded differently.
  • Reads take the file's read lock so that they are safe while other goroutines change the file. v1 read values without it, as in Key.String.

Loads read the file from disk. The mixed input is 100 sections of 100 keys with comments, inline comments, quoted values, colons, auto-increment keys and continuation lines. Measured with Go 1.27.4 on an Intel Xeon at 2.60 GHz with GOMAXPROCS=4, as the median of 8 runs.

Migrating from v1

Without options, v2 parses input as v1 does, apart from a few fixes such as # inside quoted values. The API changed: the import path is unknwon.dev/ini/v2, options are functions, package-level settings became options, and typed list methods became generic functions.

The migration guide lists every change.

Getting help

License

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

Frequently Asked Questions

What is ini?

ini is a Configuration library for the Go programming language. Go package to read and write INI files

How many GitHub stars does ini have?

ini has 3,538 GitHub stars in the directory's latest synchronization.

How do I install ini?

Install ini with the Go module system using `go get go-ini/ini`. Check the repository for the current installation instructions.

What category does ini belong to?

ini is listed under Configuration, specifically Configuration.

← Back to Configuration