GitHunt
RO

romantomjak/xconfig

Flexible configuration loader for Go applications

xconfig

Flexible configuration loader for Go applications. Supports setting struct fields from both YAML files and environment variables, with the ability to overlay environment values, apply default values, and handle custom types via encoding.TextUnmarshaler.


Features

  • Load configuration from a YAML file
  • Overlay configuration values from environment variables
  • Support for default field values via struct tags
  • Custom field parsing via encoding.TextUnmarshaler (e.g., time.Time, time.Duration)

Example

Fields can be tagged with xconfig:"key,default" to specify a different name for the key and an optional default value.

type Config struct {
	Port    int           `xconfig:",8000"`
	BindIP  net.IP        `xconfig:"bind_ip,127.0.0.1"`
	Timeout time.Duration `xconfig:",5s"`
	Debug   bool
	Hosts   []string
}

func main() {
	var cfg Config

	err := xconfig.Unmarshal(&cfg,
		// Load configuration from a file (optional)
		xconfig.WithYAMLFile("config.yml"),
		// Overlay environment values (optional)
		xconfig.WithEnvironment(os.Environ()),
	)
	if err != nil {
		slog.Error("unmarshal config", "err", err)
		os.Exit(-1)
	}

	slog.Info("loaded config", "cfg", cfg)
}

Contributing

Code patches are welcome, but to make sure things are well coordinated, please file an issue first to discuss the change before starting the work. It`s recommended that you signal your intention to contribute by filing a new issue or by claiming an existing one.

License

MIT

Languages

Go100.0%

Contributors

MIT License
Created October 2, 2025
Updated November 10, 2025
romantomjak/xconfig | GitHunt