parseini

package
v0.0.0-...-642df0c Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2024 License: BSD-3-Clause, MIT Imports: 6 Imported by: 0

README

go-ini

INI parsing library for Go (golang) This is a modified version of the original package at: github.com/vaughan0/go-ini

Usage

Parse an INI file:

package main

import (
	ini "github.com/unix-world/smartgo/parseini"
)

func main() {
	content, err := ioutil.ReadFile("myfile.ini")
	if(err == nil) {
		dat, err := ini.Load(string(content))
		if(err == nil) {
			log.Println(ini.GetIniStrVal(dat, "apples", "colour"))
			log.Println(ini.GetIniIntVal(dat, "numbers", "one")
		}
	}
}

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

[numbers]
one = 1
two = 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetIniIntVal

func GetIniIntVal(dat Data, section string, key string) int

func GetIniStrVal

func GetIniStrVal(dat Data, section string, key string) string

Types

type Data

type Data map[string]Section

A Data represents a parsed INI file.

func Load

func Load(src string) (Data, error)

Loads and returns a Data from a string.

func (Data) Get

func (f Data) 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 (Data) Load

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

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

func (Data) Section

func (f Data) Section(name string) Section

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

type ErrSyntax

type ErrSyntax struct {
	Line   int64
	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 Section

type Section map[string]string

A Section represents a single section of an INI file.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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