tinyConf

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2026 License: MIT Imports: 6 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 with support for generics and custom types.

Lines starting with # or ; are treated as comments, and empty lines are ignored.


Features

  • Generic Get[T] function for all types
  • Built-in support for: string, int, bool, time.Duration
  • Custom type converters supported
  • Default values if key is missing or conversion fails
  • List all keys via Keys()
  • No external dependencies

Installation

go get github.com/lnkssr/tinyConf
Config File Format
# Example config
; Commetn 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)

	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)
	fmt.Println("Start point:", start)
}

API

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

Custom Converters

type MyType struct { A int; B string }

converter := func(s string) (MyType, error) { ... }

value := tinyConf.Get(cfg, "my_key", MyType{}, converter)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultStringConverter   = func(s string) (string, error) { return s, nil }
	DefaultIntConverter      = func(s string) (int, error) { return strconv.Atoi(s) }
	DefaultBoolConverter     = func(s string) (bool, error) { return strconv.ParseBool(s) }
	DefaultDurationConverter = func(s string) (time.Duration, error) { return time.ParseDuration(s) }
)

Default converters

Functions

func Get

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

Get retrieves a value of type T from the config. If the key is missing or conversion fails, returns def. Optional custom converter can be provided.

Types

type Config

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

Config represents a simple key=value configuration.

func Load

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

Load reads a key=value config file from the given path and returns a Config object. Lines starting with '#' or ';' are treated as comments, and empty lines are skipped.

func (*Config) Keys

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

Keys returns all keys in the configuration.

type Converter

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

Converter is a function that converts a string to type T.

Jump to

Keyboard shortcuts

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