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:
Filemust be initialized withmake(inifile.File)before calling mutating methods (Set,Section).File.Setstores values as-is; INI parsing rules are applied byParse/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"
}