github.com/go-ini/ini parses INI configuration files, exposes section keys through typed getters, and supports writing configuration files. Load a file, select a section, then read values such as paths and booleans. section 2.2.1
Use the package to:
- Load an INI file with ini.Load and handle an unrecoverable error explicitly. section 2.2.1
- Retrieve strings with Section("Section").Key("path").String(). section 2.2.1
- Parse booleans with Bool and inspect the returned error when a value is absent, avoiding ambiguity between false and missing. section 2.2.1
- Use multiple INI files, typed defaults, and configuration writing when the application needs those capabilities. section 2.2.1
Read an INI configuration
config, err := ini.Load("conf.ini") if err != nil { fmt.Println(err) os.Exit(1) } path := config.Section("Section").Key("path").String() enabled, err := config.Section("Section").Key("enabled").Bool() if err != nil { fmt.Println(err) os.Exit(1) } fmt.Println(path, enabled) section 2.2.1
Listing 2.11 Parsing an INI configuration file
package main
import (
"fmt"
"os"
"github.com/go-ini/ini" #1
)
func main() {
config, err := ini.Load("conf.ini")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(config.Section("Section").Key("path").String()) #2
enabled, err :=
config.Section("Section").Key("enabled").Bool() #3
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(enabled) #4
}
Browse the complete runnable example for loading conf.ini, reading path, parsing enabled, and exiting on errors. section 2.2.1
