import "."
Package ini provides parsing and pretty printing methods for ini config files including comments for sections and keys. The ini data can also be loaded from/to structures using struct tags.
Since there is not really a strict definition for the ini file format, this implementation follows these rules:
- a section name cannot be empty unless it is the global one
- leading and trailing whitespaces for key names are ignored
- leading whitespace for key values are ignored
- all characters from the first non whitespace to the end of the line are
accepted for a value, unless the value is single or double quoted
- anything after a quoted value is ignored
- section and key names are not case sensitive by default
- in case of conflicting key names, only the last one is used
- in case of conflicting section names, only the last one is considered
by default. However, if specified during initialization, the keys of
conflicting sections can be merged.
Behaviour of INI processing can be modified using struct tags. The struct tags are defined by the "ini" keyword. The struct tags format is:
<key name>[,section name[,last key in a block]]
If a key name is '-' then the struct field is ignored.
- Constants
- Variables
- func Decode(r io.Reader, v interface{}) error
- func Encode(w io.Writer, v interface{}) error
- type INI
- func New(options ...Option) (*INI, error)
- func (ini *INI) Decode(v interface{}) error
- func (ini *INI) Del(section, key string) bool
- func (ini *INI) Encode(v interface{}) error
- func (ini *INI) Get(section, key string) string
- func (ini *INI) GetComments(section, key string) []string
- func (ini *INI) Has(section, key string) bool
- func (ini *INI) Keys(section string) []string
- func (ini *INI) ReadFrom(r io.Reader) (int64, error)
- func (ini *INI) Reset()
- func (ini *INI) Sections() []string
- func (ini *INI) Set(section, key, value string)
- func (ini *INI) SetComments(section, key string, comments ...string)
- func (ini *INI) WriteTo(w io.Writer) (int64, error)
- type Option
decode.go doc.go encode.go ini.go options.go read.go section.go write.go
const (
// DefaultComment is the default value used to prefix comments.
DefaultComment = ";"
// DefaultSliceSeparator is the default slice separator used to decode and encode slices.
DefaultSliceSeparator = ','
// DefaultMapKeySeparator is the default map key separator used to decode and encode slices.
DefaultMapKeySeparator = ':'
)
var DefaultOptions []Option
DefaultOptions lists the Options for the Encode and Decode functions to use.
func Decode(r io.Reader, v interface{}) error
Decode populates v with the Ini values from the Reader. DefaultOptions are used. See Ini.Decode() for more information.
func Encode(w io.Writer, v interface{}) error
Encode writes the contents of v to the given Writer. DefaultOptions are used. See Ini.Encode() for more information.
type INI struct {
// contains filtered or unexported fields
}
INI represents the content of an ini source.
func New(options ...Option) (*INI, error)
New instantiates a new Ini type ready for parsing.
func (ini *INI) Decode(v interface{}) error
Decode decodes the Ini values into v, which must be a pointer to a struct. If the struct field tag has not defined the key name then the name of the field is used. The Ini section is defined as the second item in the struct tag. Supported types for the struct fields are:
- types implementing the encoding.TextUnmarshaler interface
- all signed and unsigned integers
- float32 and float64
- string
- bool
- time.Time and time.Duration
- slices of the above types
func (ini *INI) Del(section, key string) bool
Del removes a section or key from Ini returning whether or not it did. Set the key to an empty string to remove a section.
func (ini *INI) Encode(v interface{}) error
Encode sets Ini sections and keys according to the values defined in v. v must be a pointer to a struct.
func (ini *INI) Get(section, key string) string
Get fetches the key value in the given section. If the section or the key is not found an empty string is returned.
func (*INI) GetComments
func (ini *INI) GetComments(section, key string) []string
GetComments gets the comments for the given section or key. Use an empty key to get the section comments.
func (ini *INI) Has(section, key string) bool
Has returns whether or not the section (if the key is empty) or the key exists for the given section.
func (ini *INI) Keys(section string) []string
Keys returns the list of keys for the given section.
func (ini *INI) ReadFrom(r io.Reader) (int64, error)
ReadFrom populates Ini with the data read from the reader. Leading and trailing whitespaces for the key names are removed. Leading whitespaces for key values are removed. If multiple sections have the same name, by default, the last one is used. This can be overridden with the MergeSections option.
func (ini *INI) Reset()
Reset clears all sections with their associated comments and keys. Initial Options are retained.
func (ini *INI) Sections() []string
Sections returns the list of defined sections, excluding the global one.
func (ini *INI) Set(section, key, value string)
Set adds the key with its value to the given section. If the section does not exist it is created. Setting an empty key adds a newline for the next keys.
func (*INI) SetComments
func (ini *INI) SetComments(section, key string, comments ...string)
SetComments sets the comments for the given section or key. Use an empty key to set the section comments.
func (ini *INI) WriteTo(w io.Writer) (int64, error)
WriteTo writes the contents of Ini to the given Writer.
type Option func(*INI) error
Option allows setting various options when creating an Ini type.
func CaseSensitive() Option
CaseSensitive makes section and key names case sensitive when using the Get() or Decode() methods.
func Comment(prefix string) Option
Comment sets the comment character. It defaults to ";".
func MapKeySeparator(sep rune) Option
MapKeySeparator defines the separator used to split strings when decoding or encoding a map key.
func MergeSections() Option
MergeSections merges sections when multiple ones are defined instead of overwriting them, in which case the last one wins. This is only relevant when the Ini is being initialized by ReadFrom.
func MergeSectionsWithComments() Option
MergeSectionsWithComments is equivalent to MergeSections but all the section comments merged.
func MergeSectionsWithLastComments() Option
MergeSectionsWithLastComments is equivalent to MergeSections but the section comments are set to the ones from the last section.
func SliceSeparator(sep rune) Option
SliceSeparator defines the separator used to split strings when decoding into a slice/map or encoding a slice/map into a key value.
Generated by godoc2md