liveBook · Manning

Github.com/go-ini/ini

As explained in Go in Practice, Second Edition

github.com/go-ini/ini is a Go package for parsing, accessing, and writing INI configuration files and strings.

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

How Go in Practice, Second Edition covers it

How deep it goes
Demonstrates loading, section-key access, string retrieval, Boolean parsing, and missing-value errors; defers advanced features to documentation.
Examples
Uses conf.ini with a Section section containing enabled and path keys.
What it assumes
Assumes basic Go error handling, file loading, and standard output; teaches INI sections, keys, and typed access through code.
What you can do after
Apply it via github.com/go-ini/ini to load configuration and read string and Boolean settings.
How it connects
Connects configuration files with sections, keys, typed getters, defaults, error handling, and command-line applications.

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.