Documentation
¶
Overview ¶
Package goini provides a config parser for .ini files.
In this dialect:
- Comments are lines starting with either a '#' or a ';'.
- A line ending with a '\' continues onto the next line.
- It is illegal to have a continuation before a comment or the end of file.
- All leading and trailing whitespace is stripped from properties and values.
Example (Comment) ¶
cp := NewRawConfigParser()
cp.Parse(strings.NewReader(`
message=hello
; message=world
`))
if config, err := cp.Finish(); err == nil {
messageParts := config.GlobalSection.GetPropertyValues("message")
fmt.Println(strings.Join(messageParts, " "))
}
Output: hello
Example (Continuation) ¶
cp := NewRawConfigParser()
cp.Parse(strings.NewReader(`
message=hello \
world
`))
if config, err := cp.Finish(); err == nil {
message := strings.Join(config.GlobalSection.GetPropertyValues("message"), " ")
fmt.Println(message)
}
Output: hello world
Example (GlobalSection) ¶
cp := NewRawConfigParser()
cp.Parse(strings.NewReader(`
message=hello
message=world
`))
if config, err := cp.Finish(); err == nil {
message := strings.Join(config.GlobalSection.GetPropertyValues("message"), " ")
fmt.Println(message)
}
Output: hello world
Example (Section) ¶
cp := NewRawConfigParser()
cp.Parse(strings.NewReader(`
[section]
message=hello
`))
if config, err := cp.Finish(); err == nil {
section := config.Section("section")
message := strings.Join(section.GetPropertyValues("message"), " ")
fmt.Println(message)
}
Output: hello
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DecodeOption ¶
type DecodeOption struct {
Kind DecodeOptionKind
Usage string
Parse func(string, interface{}) error
}
type DecodeOptionKind ¶
type DecodeOptionKind int
const ( UniqueOption DecodeOptionKind = iota MultiOption = iota )
type DecodeOptionSet ¶
type DecodeOptionSet map[string]*DecodeOption
func (DecodeOptionSet) Decode ¶
func (dos DecodeOptionSet) Decode(section RawSection, dest interface{}) error
Example ¶
rawSection := RawSection{
"key": []string{"1"},
}
spec := DecodeOptionSet{
"key": &DecodeOption{Kind: UniqueOption,
Usage: "A help message.",
Parse: func(v string, d interface{}) error {
i, e := strconv.Atoi(v)
if e != nil {
return e
}
*d.(*int) = i
return nil
},
},
}
var key int
spec.Decode(rawSection, &key)
fmt.Println(key)
Output: 1
type RawConfig ¶
type RawConfig struct {
GlobalSection RawSection
// contains filtered or unexported fields
}
func (*RawConfig) Section ¶
func (c *RawConfig) Section(n string) RawSection
Return the section by name or nil if the section was not defined.
type RawConfigParser ¶
type RawConfigParser struct {
// contains filtered or unexported fields
}
An object for parsing config files and building a RawConfig. Can be used to parse and merge multiple config files. Uses the "Errors are values" pattern -- intermediate steps can return error, but only the final error returned from Finish() needs to be checked.
func NewRawConfigParser ¶
func NewRawConfigParser() *RawConfigParser
func (*RawConfigParser) Finish ¶
func (cp *RawConfigParser) Finish() (*RawConfig, error)
Returns the new config object or the first error encountered while parsing.
Also resets the config parser.
func (*RawConfigParser) ParseFile ¶
func (cp *RawConfigParser) ParseFile(filename string) error
type RawSection ¶
Warning: Prefer to use the public methods since the type of RawSection might change.
func (RawSection) GetPropertyNumber ¶
func (s RawSection) GetPropertyNumber(property string) (json.Number, bool)
If the property has been set at least once, returns all values joined as a space separated string. Returns true if the propery has been set at least once.
Example ¶
cp := NewRawConfigParser()
cp.Parse(strings.NewReader(`
number=1
message=hello
message=world
`))
if config, err := cp.Finish(); err == nil {
number, _ := config.GlobalSection.GetPropertyNumber("number")
if i, e := number.Int64(); e == nil {
fmt.Println(i)
}
message, _ := config.GlobalSection.GetPropertyNumber("message")
fmt.Println(message)
}
Output: 1 hello world
func (RawSection) GetPropertyValues ¶
func (s RawSection) GetPropertyValues(property string) []string
Returns all the values set for a property or the empty list nil if has never been set.
func (RawSection) Properties ¶
func (s RawSection) Properties() []string
Returns the list of unique properties that have been set at least once.