goconf
a quick and simple library for managing dynamic configs.
made this primarily for myself for simplifying the making of my projects.
installation
you can install the lib from go get:
go get codeberg.org/dura5ka/goconf
...and include "codeberg.org/dura5ka/goconf" in your imports.
or, you can just include the main.go file somewhere in your application.
usage
// main.go
package main
import (
"fmt"
"log"
"codeberg.org/dura5ka/goconf"
)
// struct options MUST be capitalized; you should use
// struct tags to identify the options in the config.
type section1 struct {
Value1 string `mapstructure:"value1"`
Value2 int `mapstructure:"value2"`
}
type dbConf struct {
Address string `mapstructure:"address"`
Port int `mapstructure:"port"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
}
func main() {
// first, initialize the config manager.
cfgManager := goconf.New()
// second, create pointers to instances of the structs.
sect1 := §ion1{}
db := &dbConf{}
// third, register the config section(s) you'd like.
cfgManager.Register("section1", sect1)
cfgManager.Register("db", db)
// finally, load the config.
if err := cfgManager.Load("config.toml"); err != nil {
log.Printf("failed to load the config: %s\n", err)
}
// now, you can access options like this:
fmt.Printf(
"started db on %s:%d with user %s and password %s\n",
db.address,
db.port,
db.user,
db.password
)
}
# config.toml
[section1]
value1 = "hi"
value2 = 123
[db]
address = "127.0.0.1"
port = 8888
user = "database"
password = "database"
license
the project is licensed under MIT.