Skip to content
derdon edited this page Mar 28, 2013 · 7 revisions

Reading ini files

Existence checking

The method HasSection(section string) bool checks if a certain section exists in a *Config. The method HasProperty(section, property string) bool checks if a certain property exists within a given section. The following example demonstrates the usage of these two methods:

package main

import "fmt"

import "github.com/derdon/ini"

func main() {
	filecontent := "[names]\nname = alice"
	conf, err := ini.NewConfigFromString(filecontent)
	if err != nil {
		panic(err)
	}

	sectionExists := conf.HasSection("names")
	fmt.Printf("Does the section 'names' exist? %t\n", sectionExists)

	propertyNameExists := conf.HasProperty("names", "name")
	fmt.Printf("Does the property 'name' exist in the section 'names'? %t\n", propertyNameExists)

	propertyAliceExists := conf.HasProperty("names", "alice")
	fmt.Printf("Does the property 'alice' exist in the section 'names'? %t\n", propertyAliceExists)
}

Get all sections, Get all Properties of a section

To get a slice of all sections in a *Conf, there is the method GetSections() (section []string). The method GetItems(section string) (items []*Item, err error) returns a slice of pointers to Item structs. The struct Item has two fields: Property and Value, both of type string. Here is an example of how to use these methods:

package main

import "fmt"

import "github.com/derdon/ini"

func main() {
	filecontent := `[section one]
[another section]
foo = bar`
	conf, err := ini.NewConfigFromString(filecontent)
	if err != nil {
		panic(err)
	}

	// print all sections, seperated by commas
	sections := conf.GetSections()
	for i, section := range sections {
		fmt.Printf("section %#d: %q\n", i+1, section)
	}
	fmt.Println()

	// error will be nil, because we know that the passed section exists
	items, _ := conf.GetItems("section one")
	fmt.Printf("items of 'section one': %v\n\n", items)

	// print the items of the section "another section"
	items, _ = conf.GetItems("another section")
	fmt.Println("items of 'another section': ")
	for _, item := range items {
		fmt.Printf("\tproperty: %q, value: %q\n", item.Property, item.Value)
	}
}

Get specific values from a section

To get a certain value from a section by its property, use the method Get(section, property string) (value string, err error). There are also some predefined convenience methods which convert the value to a different type than string after fetching it. They are called GetBool, GetInt, GetFloat32 and GetFloat64. It is also possible to define custom converters, so called formatters. These will be covered in the following page, Custom Formatters. The following example demonstrates the usage of the method Get and the built-in formatter methods:

package main

import "fmt"

import "github.com/derdon/ini"

func main() {
	filecontent := `[section]
goethe quote = Da steh ich nun, ich armer Tor! Und bin so klug als wie zuvor.
sense of life = 42
sqrt of two = 1.41421356237
is this a boolean = true
`
	conf, err := ini.NewConfigFromString(filecontent)
	if err != nil {
		panic(err)
	}
	// We know that both the section and the property exist,
	// so the error value can be discarded
	value, _ := conf.Get("section", "goethe quote")
	fmt.Printf("the value of 'goethe quote' is: %q\n", value)

	integer, _ := conf.GetInt("section", "sense of life")
	fmt.Printf("the value of 'sense of life' is: %d\n", integer)

	float, _ := conf.GetFloat32("section", "sqrt of two")
	fmt.Printf("the value of 'sqrt of two' is: %f\n", float)

	boolean, _ := conf.GetBool("section", "is this a boolean")
	fmt.Printf("the value of 'is this a boolean' is: %t\n", boolean)
}