liveBook · Manning

INI

As explained in Go in Practice, Second Edition

INI is a simple text configuration-file format organized into named sections containing key-value settings.

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

How Go in Practice, Second Edition covers it

How deep it goes
Demonstrates file loading, section access, typed getters, and missing-boolean handling without covering INI writing.
Examples
Uses a Section containing enabled and path settings, plus a complete Go parsing program.
What you can do after
Operate INI configuration through github.com/go-ini/ini, retrieving strings and booleans while checking conversion errors.
How it connects
Connects configuration files with JSON, YAML, third-party libraries, and typed value conversion.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.