-
Notifications
You must be signed in to change notification settings - Fork 0
Writing
There is one *Config
constructor I haven't talked about yet: NewConfig() *Config
. It takes no parameters and returns a new empty config. An error is never returned; the only returned value is a pointer to the created Config.
The method AddSection(section string) error
adds a new section to the config. If the passed section already exists, DuplicateSectionError
will be returned and the old section will not be overwritten.
RemoveSection(section string) error
removes the given section from the config if it exists. Otherwise, NoSectionError
will be returned.
Set(section, property, value)
adds a new property / value pair to a section or overwrites the property with the given value if it already exists. If the section does not exist, NoSectionError
is returned.
RemoveProperty(section, property string)
removes the given property from the section if both the section and the property exist. If the section does not exist, NoSectionError
is returned. If the property does not exist, NoPropertyError
is returned.
This commented example demonstrates the creation of an empty config and how to add and remove section and properties. Finally, the config is written to standard output via fmt.Println
. Of course, you can also print the config to a file. You can get the content with fmt.Sprint(conf)
or conf.String()
package main
import "fmt"
import "github.com/derdon/ini"
func main() {
// create a new empty config
conf := ini.NewConfig()
// add a section
conf.AddSection("my little section")
// and another
conf.AddSection("temporary section")
// ... and remove it
conf.RemoveSection("temporary section")
// create a new property and initialise it with a value
conf.Set("my little section", "temp property", "temp value")
// ... and remove it
conf.RemoveProperty("my little section", "temp property")
// config now only contains the section "my little section" and no items
fmt.Println(conf)
}