tinyConf

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

tinyConf

tinyConf is a lightweight Go library for reading plain text key=value configuration files.
It is designed for small, simple configurations where complex formats like YAML/TOML/JSON are unnecessary.

The library is deterministic, minimal, and easy to reason about.

Lines starting with # or ; are treated as comments.
Empty lines are ignored.

Features

  • Plain key=value configuration format
  • Generic access via Get[T]
  • Built-in support for:
    • string
    • int
    • bool
    • time.Duration
  • Support for custom types via converters
  • Safe defaults on missing keys or conversion errors
  • Required keys validation
  • Reload config from disk
  • List all keys
  • Dump config for debugging
  • No struct tags
  • No reflection-based magic
  • No external dependencies

Installation

go get github.com/lnkssr/tinyConf

Config File Format

# Example config
; comment variant

work_dir = /var/lib/videos
index_file = ./index.html
max_items = 50
debug = true
timeout = 30s
start_point = 100,200

Usage

package main

import (
	"errors"
	"fmt"
	"log"
	"strconv"
	"strings"
	"time"

	"github.com/lnkssr/tinyConf"
)

func main() {
	cfg, err := tinyConf.Load("config")
	if err != nil {
		log.Fatal(err)
	}

	videoDir := tinyConf.Get(cfg, "work_dir", "/tmp/videos")
	indexFile := tinyConf.Get(cfg, "index_file", "./index.html")
	maxItems := tinyConf.Get(cfg, "max_items", 10)
	debugMode := tinyConf.Get(cfg, "debug", false)
	timeout := tinyConf.Get(cfg, "timeout", 5*time.Second)

	fmt.Println(videoDir, indexFile, maxItems, debugMode, timeout)
}

Required Keys

port, err := tinyConf.Require[int](cfg, "port")
if err != nil {
	log.Fatal(err)
}

Require[T] returns an error if:

  • the key is missing
  • the value cannot be converted

Custom Types

type Point struct{ X, Y int }

pointConverter := func(s string) (Point, error) {
	parts := strings.Split(s, ",")
	if len(parts) != 2 {
		return Point{}, errors.New("invalid format")
	}
	x, err := strconv.Atoi(parts[0])
	if err != nil {
		return Point{}, err
	}
	y, err := strconv.Atoi(parts[1])
	if err != nil {
		return Point{}, err
	}
	return Point{X: x, Y: y}, nil
}

start := tinyConf.Get(cfg, "start_point", Point{X: 0, Y: 0}, pointConverter)

Reload Configuration

err := cfg.Reload()
if err != nil {
	log.Fatal(err)
}

Reload rereads the config file from disk and atomically replaces values.

Debugging

cfg.Dump()

Prints all key=value pairs to stdout. Intended for debugging only.

API

Load(path string) (*Config, error)

Loads a config file into memory.

Get[T](cfg *Config, key string, def T, converter ...Converter[T]) T

Returns a value of type T.

  • Returns def if key is missing
  • Returns def if conversion fails
  • Optional converter enables custom types
Require[T](cfg *Config, key string, converter ...Converter[T]) (T, error)

Returns a value of type T or an error if the key is missing or invalid.

Reload() error

Reloads the configuration file from disk.

Keys() []string

Returns all configuration keys.

Dump()

Prints all config values for debugging.

Design Philosophy

  • Small configs only
  • No nested structures
  • No environment variable injection
  • No implicit behavior
  • Explicit is better than clever
  • Easy to remove or replace

If you need advanced features - use TOML, YAML, or JSON. If you need a simple config - use tinyConf.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get[T any](cfg *Config, key string, def T, conv ...Converter[T]) T

Get returns value converted to T or default

func Require added in v0.2.0

func Require[T any](cfg *Config, key string, conv ...Converter[T]) (T, error)

Require returns value or error if missing or invalid

Types

type Config

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

Config holds parsed config data

func Load

func Load(path string) (*Config, error)

Load reads key=value config file into memory

func (*Config) Dump added in v0.2.0

func (c *Config) Dump()

Dump prints all key=value pairs (debug only)

func (*Config) Keys

func (c *Config) Keys() []string

Keys returns all keys

func (*Config) Reload added in v0.2.0

func (c *Config) Reload() error

Reload rereads config file from disk

type Converter

type Converter[T any] func(string) (T, error)

Converter converts string value to type T

Jump to

Keyboard shortcuts

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