Simplified configuration for Go projects.
Automatically loads config files, .env
, environment variables and command line arguments.
Allows you to define profiles, automatically loading files with the defined profile (e.g. config.yaml
, config-dev.yaml
, config-prod.yaml
).
Docs: https://pkg.go.dev/github.com/go-path/cfg
If you have a config file named config.json
or confgi.yaml
in the project root directory. Do.
var config = cfg.Global()
func init() {
if err := config.Load(); err != nil {
slog.Error(err.Error(), slog.Any("error", err))
os.Exit(1)
}
slog.Info("My db connection:" + config.String("app.db.host"))
}
config.Load()
initialize default settings. A variable is obtained respecting the order below.
- command line arguments (starting with "--", e.g.
--server.port=9000
) - DotEnv file variables
.env
- Operating system variables
- Profile specific configuration (
config-{dev|prod|test}.{json,yaml,yml}
) - global config (
config.{json,yaml,yml}
) - Default config (cfg.New(DefaultConfig))
If you want to change the configuration file name, use the
SetFilePaths
method (default is"config"
)
- config.Get(key string) any
- config.Bool(key string) bool
- config.Int(key string, def ...int) int
- config.Float(key string, def ...float64) float64
- config.String(key string, def ...string) string
- config.Strings(key string, def ...[]string) []string
- config.Duration(key string, def ...time.Duration) time.Duration
- config.Time(key string, def ...time.Time) time.Time
- config.DateTime(key string, def ...time.Time) time.Time
- config.DateOnly(key string, def ...time.Time) time.Time
- config.TimeOnly(key string, def ...time.Time) time.Time
- config.TimeLayout(key string, layout string, def ...time.Time) time.Time
- config.Keys(key string) []string
- config.Set(key string, value any)
- config.SetString(key string, value string)
- config.SetFileSystem(fs http.FileSystem)
- config.SetFilePaths(filePaths ...string)
- config.SetFileExt(ext string, fn UnmarshalFn)
- config.SetProfileKey(profileKey string)
- config.Load() error
- config.LoadOsArgs(args []string)
- config.LoadOsEnv()
- config.LoadDotEnv() error
- config.LoadEnviron(environ []string)
- config.LoadObject(config O)
- config.LoadFiles() error
- config.LoadProfiles() error
- config.Clone() *Env
- config.Merge(src *Env)