ini

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2015 License: MIT, MPL-2.0 Imports: 6 Imported by: 0

README

go-ini

INI parsing library for Go (golang).

View the API documentation here.

Usage

Parse an INI file:

import "github.com/vaughan0/go-ini"

file, err := ini.LoadFile("myfile.ini")

Get data from the parsed file:

name, ok := file.Get("person", "name")
if !ok {
  panic("'name' variable missing from 'person' section")
}

Iterate through values in a section:

for key, value := range file["mysection"] {
  fmt.Printf("%s => %s\n", key, value)
}

Iterate through sections in a file:

for name, section := range file {
  fmt.Printf("Section name: %s\n", name)
}

File Format

INI files are parsed by go-ini line-by-line. Each line may be one of the following:

  • A section definition: [section-name]
  • A property: key = value
  • A comment: #blahblah or ;blahblah
  • Blank. The line will be ignored.

Properties defined before any section headers are placed in the default section, which has the empty string as it's key.

Example:

# I am a comment
; So am I!

[apples]
colour = red or green
shape = applish

[oranges]
shape = square
colour = blue

Documentation

Overview

Package ini provides functions for parsing INI configuration files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrSyntax

type ErrSyntax struct {
	Line   int
	Source string // The contents of the erroneous line, without leading or trailing whitespace
}

ErrSyntax is returned when there is a syntax error in an INI file.

func (ErrSyntax) Error

func (e ErrSyntax) Error() string

type File

type File map[string]Section

A File represents a parsed INI file.

func Load

func Load(in io.Reader) (File, error)

Loads and returns a File from a reader.

func LoadFile

func LoadFile(filename string) (File, error)

Loads and returns an INI File from a file on disk.

func (File) Get

func (f File) Get(section, key string) (value string, ok bool)

Looks up a value for a key in a section and returns that value, along with a boolean result similar to a map lookup.

func (File) Load

func (f File) Load(in io.Reader) (err error)

Loads INI data from a reader and stores the data in the File.

func (File) LoadFile

func (f File) LoadFile(file string) (err error)

Loads INI data from a named file and stores the data in the File.

func (File) Section

func (f File) Section(name string) Section

Returns a named Section. A Section will be created if one does not already exist for the given name.

type Section

type Section map[string]string

A Section represents a single section of an INI file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL