INI is a readable text configuration format organized into named sections and key-value settings. Go has no built-in INI processor, so applications commonly use the github.com/go-ini/ini package. section 2.2.1
Parse an INI configuration
A file such as [Section] with enabled = true and path = /usr/local can be loaded with ini.Load. Access values through config.Section("Section").Key("path").String(), or convert enabled with Key("enabled").Bool(). Check the returned error because a false value may mean either an explicit false or a missing setting. section 2.2.1
; A comment line [Section] enabled = true path = /usr/local # another comment
Shows INI comments, a named section, boolean and path settings, and an inline comment. 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
}
Provides a complete Go program that loads an INI file, reads string and boolean values, and exits on errors. section 2.2.1
