Documentation ¶
Overview ¶
Package ini parses an ini formatted file. The parsed configuration can be decoded into a variable or used directly.
Index ¶
Examples ¶
Constants ¶
const Global = ""
Global is the section name for key-values not under a section. It is used for retrieving key-value pairs in the "global" section, or not under any other section.
value := config[ini.Global]["key"] value2, found := config[ini.Global]["key2"]
Variables ¶
This section is empty.
Functions ¶
func DecodeValue ¶
DecodeValue decodes a single configuration value into a variable.
func IsCovertionError ¶
IsCovertionError checks if an error is a covertion error.
func IsOverflowError ¶
IsOverflowError checks if an error is an overflow error.
func IsSyntaxError ¶
IsSyntaxError checks if an error is a syntax error.
Types ¶
type Config ¶
Config holds all key-value pairs under sections. To retrieve a key-value pair you can use:
value := config["section"]["key"] value2, found := config["section"]["key2"]
The same as you would for a map. For retrieving key-value pairs not in any section, or the "global" section, you can use the `ini.Global` constant, like so:
value := config[ini.Global]["key"] value2, found := config[ini.Global]["key2"]
func Parse ¶
Parse parses ini formatted input.
Note: the reader already gets buffered, so there is no need to buffer it yourself.
Example ¶
r := strings.NewReader("key = value") conf, err := Parse(r) if err != nil { panic(err) } value := conf[Global]["key"] fmt.Println("value:", value) value, found := conf[Global]["unknown"] fmt.Println("found:", found) fmt.Println("value:", value)
Output: value: value found: false value:
Example (Section) ¶
confFile := "key = value\n" + "[section1]\n" + "key = value2" r := strings.NewReader(confFile) conf, err := Parse(r) if err != nil { panic(err) } value := conf[Global]["key"] value2 := conf["section1"]["key"] fmt.Println("value:", value) fmt.Println("value2:", value2)
Output: value: value value2: value2
func (*Config) Decode ¶
Decode decodes a configuration into a struct. Any properties to be set need to be public. Keys are renamed, whitespace is removed and keys start with a capaital, like so:
"my key" -> "MyKey"
Tags are supported to define the key of a configuration. See below for a small example, for more examples see the tags example in the _examples directory.
struct { AppName `ini:"name"` }
Slices are supported by using a comma separated list, like so:
"string1, string2" -> []string{"string1", "string2"} "1, 2, 3" -> []int{1, 2, 3}
Booleans are supported as well:
"1, t, T, TRUE, true, True" -> true "0, f, F, FALSE, false, False" -> false
Time is supported with the following formats:
"2006-01-02", "2006-01-02 15:04", "2006-01-02 15:04:05", time.RFC3339, time.RFC1123 and time.RFC822
Duration is also supported, see `time.ParseDuration` for the documentation.
Note: underneath Decode uses the reflect package which isn't great for performance, so use it with care.