Powerful configuration management in Go
- Load configuration files from standard system paths
- Support for YAML, JSON, and XML formats
- File watching for automatic configuration reloading
- Cross-platform support (Unix, Linux, macOS, Windows)
go get github.com/LimpidTech/prefer.gopackage main
import (
"fmt"
"log"
"github.com/LimpidTech/prefer.go"
)
type Config struct {
Name string `yaml:"name"`
Port int `yaml:"port"`
}
func main() {
var config Config
// Load config from standard paths (./config.yaml, /etc/config.yaml, etc.)
cfg, err := prefer.Load("config", &config)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Loaded from: %s\n", cfg.Identifier)
fmt.Printf("Name: %s, Port: %d\n", config.Name, config.Port)
}package main
import (
"fmt"
"log"
"github.com/LimpidTech/prefer.go"
)
type Config struct {
Name string `yaml:"name"`
}
func main() {
var config Config
// Watch for configuration changes
channel, err := prefer.Watch("config", &config)
if err != nil {
log.Fatal(err)
}
for updatedConfig := range channel {
cfg := updatedConfig.(*Config)
fmt.Printf("Config updated: %s\n", cfg.Name)
}
}- YAML (
.yaml,.yml) - JSON (
.json) - XML (
.xml) - INI (
.ini)
The library searches for configuration files in the following locations (in order):
- Current directory (
.) - Working directory
$XDG_CONFIG_DIRS$HOME/.config$HOME/usr/local/etc/usr/etc/etc
- Current directory
%USERPROFILE%%LOCALPROFILE%%APPDATA%%CommonProgramFiles%%ProgramData%%ProgramFiles%%SystemRoot%
- Go 1.25 or later
See LICENSE file for details.