ini

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2025 License: MIT Imports: 5 Imported by: 0

README

GoDoc

INI File Parser for Go

A robust INI file handler in Go that supports common INI file features.

Features

  • Simple and intuitive API
  • Implements standard Go interfaces (io.ReaderFrom and io.WriterTo)
  • Support for different comment styles (; and #)
  • Handling of quoted values (both single and double quotes)
  • Thread-safe option via IniSafe wrapper
  • Section and key management (add, get, set, delete)
  • Case-insensitive keys and sections
  • Error reporting with line numbers
  • Auto-quoting of values with spaces

Basic Usage

package main

import (
	"fmt"
	"os"

	"github.com/KarpelesLab/ini"
)

func main() {
	// Create a new INI structure
	config := ini.New()

	// Load from a file (two equivalent ways)
	file, _ := os.Open("config.ini")
	defer file.Close()
	
	// Using io.ReaderFrom interface (preferred)
	config.ReadFrom(file)
	
	// Or using the legacy method
	// config.Load(file)

	// Get values
	if value, ok := config.Get("section", "key"); ok {
		fmt.Printf("Value: %s\n", value)
	}

	// Get with default fallback
	value := config.GetDefault("section", "missing_key", "default_value")
	fmt.Printf("Value with default: %s\n", value)

	// Set values
	config.Set("newsection", "newkey", "newvalue")

	// Check if a section exists
	if config.HasSection("section") {
		fmt.Println("Section exists!")
	}

	// Write to a file (two equivalent ways)
	outFile, _ := os.Create("newconfig.ini")
	defer outFile.Close()
	
	// Using io.WriterTo interface (preferred)
	config.WriteTo(outFile)
	
	// Or using the legacy method
	// config.Write(outFile)
}

Standard Go Interfaces

The library implements the standard Go interfaces:

  • io.ReaderFrom: Parse INI files with ReadFrom(r io.Reader) (n int64, err error)
  • io.WriterTo: Write INI files with WriteTo(w io.Writer) (n int64, err error)

These interfaces make the library more idiomatic and composable with other Go libraries.

Thread-Safety

For thread-safe operations, use the IniSafe wrapper:

// Create a thread-safe INI structure
config := ini.NewThreadSafe()

// Use the same API methods
config.Set("section", "key", "value")
value, ok := config.Get("section", "key")

// Use standard interfaces
file, _ := os.Open("config.ini")
config.ReadFrom(file)

outFile, _ := os.Create("output.ini")
config.WriteTo(outFile)

Section Management

// Check if section exists
if config.HasSection("mysection") {
    // Get all section names
    sections := config.Sections()
    
    // Get all keys in a section
    keys := config.Keys("mysection")
}

License

This library is available under the LICENSE file in the repository.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Ini

type Ini map[string]map[string]string

Ini represents an INI file with sections and key-value pairs. Not thread-safe by default; use IniSafe for concurrent access.

func New

func New() Ini

New returns a new Ini structure.

func (Ini) Get

func (i Ini) Get(section, key string) (string, bool)

Get returns a value for a given key. Use section "root" for entries at the beginning of the file.

func (Ini) GetDefault added in v0.1.0

func (i Ini) GetDefault(section, key, defaultValue string) string

GetDefault returns a value for a given key or the provided default if not found.

func (Ini) HasSection added in v0.1.0

func (i Ini) HasSection(section string) bool

HasSection checks if a section exists.

func (Ini) Keys added in v0.1.0

func (i Ini) Keys(section string) []string

Keys returns a list of all keys in a section.

func (Ini) Load

func (i Ini) Load(source io.Reader) error

Load will parse source and merge loaded values. Deprecated: Use ReadFrom instead which implements io.ReaderFrom interface.

func (Ini) ReadFrom added in v0.1.0

func (i Ini) ReadFrom(source io.Reader) (int64, error)

ReadFrom implements the io.ReaderFrom interface. It parses the source and merges loaded values, returning the number of bytes read and any error.

func (Ini) Sections added in v0.1.0

func (i Ini) Sections() []string

Sections returns a list of all section names.

func (Ini) Set

func (i Ini) Set(section, key, value string)

Set changes a value in the ini file.

func (Ini) Unset

func (i Ini) Unset(section, key string)

Unset removes a value from the ini file.

func (Ini) Write

func (i Ini) Write(d io.Writer) error

Write generates an ini file and writes it to the provided output. Deprecated: Use WriteTo instead which implements io.WriterTo interface.

func (Ini) WriteTo added in v0.1.0

func (i Ini) WriteTo(d io.Writer) (int64, error)

WriteTo implements the io.WriterTo interface. It generates an ini file and writes it to the provided output, returning the number of bytes written and any error.

type IniSafe added in v0.1.0

type IniSafe struct {
	// contains filtered or unexported fields
}

IniSafe is a thread-safe wrapper around Ini.

func NewThreadSafe added in v0.1.0

func NewThreadSafe() *IniSafe

NewThreadSafe returns a new thread-safe Ini structure.

func (*IniSafe) Get added in v0.1.0

func (i *IniSafe) Get(section, key string) (string, bool)

Get returns a value for a given key in a thread-safe manner.

func (*IniSafe) GetDefault added in v0.1.0

func (i *IniSafe) GetDefault(section, key, defaultValue string) string

GetDefault returns a value or default in a thread-safe manner.

func (*IniSafe) HasSection added in v0.1.0

func (i *IniSafe) HasSection(section string) bool

HasSection checks if a section exists in a thread-safe manner.

func (*IniSafe) Keys added in v0.1.0

func (i *IniSafe) Keys(section string) []string

Keys returns all keys in a section in a thread-safe manner.

func (*IniSafe) Load added in v0.1.0

func (i *IniSafe) Load(source io.Reader) error

Load parses source and merges loaded values in a thread-safe manner. Deprecated: Use ReadFrom instead which implements io.ReaderFrom interface.

func (*IniSafe) ReadFrom added in v0.1.0

func (i *IniSafe) ReadFrom(source io.Reader) (int64, error)

ReadFrom implements the io.ReaderFrom interface with thread safety.

func (*IniSafe) Sections added in v0.1.0

func (i *IniSafe) Sections() []string

Sections returns all section names in a thread-safe manner.

func (*IniSafe) Set added in v0.1.0

func (i *IniSafe) Set(section, key, value string)

Set changes a value in a thread-safe manner.

func (*IniSafe) Unset added in v0.1.0

func (i *IniSafe) Unset(section, key string)

Unset removes a value in a thread-safe manner.

func (*IniSafe) Write added in v0.1.0

func (i *IniSafe) Write(d io.Writer) error

Write generates an ini file and writes it to the provided output in a thread-safe manner. Deprecated: Use WriteTo instead which implements io.WriterTo interface.

func (*IniSafe) WriteTo added in v0.1.0

func (i *IniSafe) WriteTo(d io.Writer) (int64, error)

WriteTo implements the io.WriterTo interface with thread safety.

Jump to

Keyboard shortcuts

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