inifile

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: MIT Imports: 9 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

build coverage goreport Docs

inifile

Simple INI file reader.

Section and key names are case-insensitive and ignore leading and trailing whitespace.

Supports line comments, trailing comments and quoted values. Unquoted values are whitespace trimmed; quoted values preserve whitespace inside quotes. Parsed values are always valid UTF-8. Line endings should be LF (\n) or CRLF (\r\n). A trailing CR-only (\r) line ending on the final line is tolerated, but bare \r elsewhere is treated as a syntax error. Lines are limited by bufio.Scanner's default token size (about 64 KiB per line); longer lines fail with scanner errors (for example bufio.ErrTooLong). Scanner/read errors are returned as-is and no File is returned.

Keys appearing more than once will either replace previous values or append to them with a separator.

API notes:

  • File must be initialized with make(inifile.File) before calling mutating methods (Set, Section).
  • File.Set stores values as-is; INI parsing rules are applied by Parse/Load.
func ExampleParse() {
	const initext = `
# comments start with a hash
; or a semicolon

# global keys are in the unnamed (empty string) section
Username = " user name " # values can be quoted preserve whitespace or embed quotes

# section names are case insensitive and ignores leading and trailing whitespace
[ HTTP ] 
 port = 8080 # keys and unquoted values are stripped of leading and trailing whitespace
port=8081 # keys appearing more than once either append or replace values
`

	inif, err := inifile.Parse(strings.NewReader(initext), ',')
	if err != nil {
		fmt.Fprint(os.Stderr, err)
	}
	username, _ := inif.Get("", "username")
	ports, _ := inif.Get("http", "port")

	fmt.Printf("%q\n%q\n", username, ports)
	// Output:
	// " user name "
	// "8080,8081"
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Key added in v1.0.0

func Key(s string) string

Key returns s lowercased (using strings.ToLower) and with whitespace trimmed. This is not full Unicode case folding.

func ParseValue added in v1.0.0

func ParseValue(s string) (value string, err error)

ParseValue parses a value string from an INI-file, unquoting and removing trailing comments if needed. Parsed values are always valid UTF-8.

Types

type File added in v1.0.0

type File map[string]Section

A File represents a parsed INI file. Values found outside of a named section are in the "" section.

The map keys are all lowercase and trimmed of whitespace.

The zero value of File is read-only. Initialize with make(File) before calling Set or Section.

func Load added in v1.0.0

func Load(fn string, dupKeysJoin rune) (inif File, err error)

Load reads INI data from the given file and calls Parse on it.

func Parse added in v1.0.0

func Parse(r io.Reader, dupKeysJoin rune) (inif File, err error)

Parse reads INI data from an io.Reader and returns a new File.

Unquoted values are trimmed of whitespace. Quoted values preserve whitespace inside the quotes. Parsed values are always valid UTF-8. Input should use LF (\n) or CRLF (\r\n) line endings. A trailing CR-only line ending on the final line is tolerated, but bare CR (\r) characters elsewhere are rejected as syntax errors. Lines are limited by bufio.Scanner's default token size (~64 KiB).

If dupKeysJoin is zero, a duplicate key will replace the previous value. If dupKeysJoin is nonzero, a duplicate key will append it's value to the preexisting key's value using dupKeysJoin as a separator.

You are allowed to pass in a nil io.Reader, which results in a nil File and no error.

Example
package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/linkdata/inifile"
)

func main() {
	const initext = `
# comments start with a hash
; or a semicolon

# global keys are in the unnamed (empty string) section
Username = " user name " # values can be quoted preserve whitespace or embed quotes

# section names are case insensitive and ignores leading and trailing whitespace
[ HTTP ] 
 port = 8080 # keys and values are stripped of leading and trailing whitespace
port=8081 # keys appearing more than once either append or replace values
`

	inif, err := inifile.Parse(strings.NewReader(initext), ',')
	if err != nil {
		fmt.Fprint(os.Stderr, err)
	}
	username, _ := inif.Get("", "username")
	ports, _ := inif.Get("http", "port")

	fmt.Printf("%q\n%q\n", username, ports)
}
Output:
" user name "
"8080,8081"

func (File) Get added in v1.0.0

func (inif File) Get(section, key string) (value string, ok bool)

Get looks up a value for a key in a section and returns that value, along with a boolean result similar to a map lookup.

Section and key strings are case-insensitive and ignore leading and trailing whitespace.

func (File) GetDefault added in v1.0.0

func (inif File) GetDefault(section, key, dflt string) string

GetDefault calls Get but returns dflt if the section or key was not found.

Section and key strings are case-insensitive and ignore leading and trailing whitespace.

func (File) Section added in v1.0.0

func (inif File) Section(name string) (section Section)

Section returns a named Section. A Section will be created if one does not already exist for the given name.

Section and key strings are case-insensitive and ignore leading and trailing whitespace.

inif must be initialized with make(File). Calling Section on a nil File panics.

func (File) Set added in v1.0.0

func (inif File) Set(section, key, value string, join rune)

Set sets the value for a key in a section.

The section is created if needed. The value is stored as-is. Use Parse to apply INI parsing rules (such as trimming unquoted values). If join is nonzero, the value is appended to any pre-existing value using join as the separator.

Section and key strings are case-insensitive and ignore leading and trailing whitespace.

inif must be initialized with make(File). Calling Set on a nil File panics.

type Section added in v1.0.0

type Section map[string]string

A Section represents a single section of an INI file.

The map keys are all lowercase and trimmed of whitespace.

func (Section) Get added in v1.0.0

func (sect Section) Get(key string) (value string, ok bool)

Get looks up a value for a key in a section and returns that value, along with a boolean result similar to a map lookup.

Key strings are case-insensitive and ignore leading and trailing whitespace.

func (Section) GetDefault added in v1.0.0

func (sect Section) GetDefault(key, dflt string) string

GetDefault calls Get but returns dflt if the key was not found.

Key strings are case-insensitive and ignore leading and trailing whitespace.

func (Section) Set added in v1.0.0

func (sect Section) Set(key, value string, join rune)

Set sets the value for a key in a section.

If join is nonzero, the value is appended to any pre-existing value using join as the separator.

Key strings are case-insensitive and ignore leading and trailing whitespace. Panics if sect is nil.

type SyntaxError added in v1.0.0

type SyntaxError struct {
	Line   int    // Line number where the error was found
	Source string // The contents of the erroneous line, without leading or trailing whitespace
	Err    error  // Optional source error
}
var ErrSyntax SyntaxError

ErrSyntax is returned when there is a syntax error in an INI file.

func (SyntaxError) Error added in v1.0.0

func (e SyntaxError) Error() string

func (SyntaxError) Is added in v1.0.0

func (e SyntaxError) Is(target error) bool

func (SyntaxError) Unwrap added in v1.0.2

func (e SyntaxError) Unwrap() error

Jump to

Keyboard shortcuts

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