goconfig is a simple and lightweight configuration management library for Go applications. It provides an easy way to load, save, and create configuration files using structs.
Currently, it supports JSON format, with plans to support YAML and TOML in the future.
- Load: Read configuration from a file into a struct.
- Save: Write configuration from a struct into a file.
- Create: Initialize a new configuration file from a struct (creates directories if they don't exist).
- Type Safety: Uses Go structs and reflection to ensure type safety.
go get github.com/nightails/goconfigtype Config struct {
Port int `json:"port"`
DBHost string `json:"db_host"`
Debug bool `json:"debug"`
}cfg := &Config{}
err := goconfig.Load("config.json", cfg)
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}cfg := &Config{Port: 8080, DBHost: "localhost", Debug: true}
err := goconfig.Save("config.json", cfg)
if err != nil {
log.Fatalf("Failed to save config: %v", err)
}defaultCfg := &Config{Port: 8080, DBHost: "localhost"}
err := goconfig.Create("config/settings.json", defaultCfg)
if err != nil {
log.Fatalf("Failed to create config: %v", err)
}- Multi-format Support: Add support for YAML, TOML, and HCL.
- Environment Variable Overrides: Allow overriding struct fields using environment variables (e.g.,
APP_PORT=9000). - Validation: Integrate with
go-playground/validatorto validate configuration values on load. - Default Values: Support default value tags in structs.
- Hot Reloading: Watch configuration files for changes and automatically reload.
- Remote Config: Support loading configuration from Etcd, Consul, or AWS Secrets Manager.
- Command Line Flags: Merge configuration with CLI flags.
This project is licensed under the MIT License - see the LICENSE file for details.