config

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 11 Imported by: 4

README

config

A tiny INI-style config file package for Go, with live reload.

Install

go get github.com/eqto/config

File format

# comments start with # or //

[server]
host = localhost
port = 8080

# repeat a key for a list of values
[server]
domain = example.com
domain = example.org

# prefix a section with - for an array of objects
-[database]
host = db1
port = 5432

-[database]
host = db2
port = 5432

Usage

Package-level functions operate on a shared file opened with Open:

if err := config.Open("configs/app.conf"); err != nil {
    log.Fatal(err)
}

host := config.Get("server.host")          // "localhost"
port := config.GetInt("server.port")       // 8080
name := config.GetOr("server.name", "app") // default when missing

domains := config.GetStringArray("server.domain") // ["example.com", "example.org"]

for _, db := range config.GetArray("database") {
    fmt.Println(db.Get("host"), db.GetInt("port"))
}

Changes made with Put can be written back with Save:

config.Put("server.port", "9090")
config.Save()
Live reload

Parsed files are watched automatically. Register a callback to be notified when the file changes on disk:

config.OnChanges(func() {
    log.Println("config reloaded")
})

To watch a file in any format without parsing it, use Watch and keep the handle so you can stop it later:

w := config.Watch("data.json", func() {
    log.Println("data.json changed")
})
defer w.Close()
Multiple files

Use ParseFile for independent instances instead of the shared one:

cfg, err := config.ParseFile("other.conf")
if err != nil {
    log.Fatal(err)
}
defer cfg.Close()
fmt.Println(cfg.Get("server.host"))

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get added in v0.3.0

func Get(key string) string

Get retrieve string value by key or return empty string if not found

func GetInt added in v0.3.0

func GetInt(key string) int

func GetIntOr added in v0.3.0

func GetIntOr(key string, def int) int

func GetOr added in v0.3.0

func GetOr(key, def string) string

GetOr retrieve string value by key or return def value if not found

func GetStringArray added in v0.5.1

func GetStringArray(key string) []string

func OnChanges added in v0.4.0

func OnChanges(fun func())

func Open added in v0.3.0

func Open(filename string) error

func Put added in v0.3.0

func Put(key string, value string)

func Save added in v0.3.0

func Save() error

func String added in v0.3.0

func String() string

Types

type File added in v0.3.0

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

func New added in v0.3.0

func New() *File

func NewFile added in v0.3.0

func NewFile(filename string) *File

func ParseFile

func ParseFile(filename string) (*File, error)

func Watch added in v0.6.0

func Watch(filename string, onChange func()) *File

Watch monitors filename for write/remove/rename events and invokes onChange on each — format-agnostic: unlike ParseFile it doesn't parse the contents, so it can watch files in any format. The returned File is just the lifecycle handle — Close() stops the watcher.

func (*File) Close added in v0.5.0

func (f *File) Close()

Close stops the file watcher.

func (*File) Get added in v0.3.0

func (f *File) Get(key string) string

Get retrieve string value by key or return empty string if not found

func (*File) GetArray added in v0.3.0

func (f *File) GetArray(key string) []*Node

func (*File) GetInt added in v0.3.0

func (f *File) GetInt(key string) int

func (*File) GetIntOr added in v0.3.0

func (f *File) GetIntOr(key string, def int) int

func (*File) GetOr added in v0.3.0

func (f *File) GetOr(key, def string) string

GetOr retrieve string value by key or return def value if not found

func (*File) GetStringArray added in v0.5.0

func (f *File) GetStringArray(key string) []string

func (*File) OnChanges added in v0.4.0

func (f *File) OnChanges(fun func())

func (*File) Put added in v0.3.0

func (f *File) Put(key string, value string) *File

func (*File) Save added in v0.3.0

func (f *File) Save() error

func (*File) String added in v0.3.0

func (f *File) String() string

type Node added in v0.2.0

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

func GetArray added in v0.3.0

func GetArray(key string) []*Node

func (*Node) Get added in v0.2.0

func (n *Node) Get(key string) string

func (*Node) GetInt added in v0.2.0

func (n *Node) GetInt(key string) int

func (*Node) String added in v0.2.0

func (n *Node) String() string

Jump to

Keyboard shortcuts

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