配置管理
ling-base common/config 模块文档
在线 Playground
在浏览器中直接体验本页相关 API,无需本地安装 Go 环境。
config
Multi-format, multi-environment configuration loader with YAML and .env support, env-specific overrides, and a database-backed key/value store.
Features
- Layered loading: base file -> env-specific file -> OS env var overrides
- YAML and .env file formats (auto-detected by extension)
- Struct tag-based env var binding (
env:"KEY"/env:"KEY,required") Store-- database-backed config with LRU cache and env fallback
Key types
Loader-- builder-style config loaderFormat-- file format (FormatAuto,FormatYAML,FormatENV)Store-- system config store with DB persistence and cacheConfigItem-- GORM model for theconfigstableStoreOptions-- store configuration (DB, cache, TTL)
Key functions
Load(dir, env, out)-- convenience loaderNew()-- builder API (Dir,Env,BaseName,Format,WithEnvVars)LoadYAML(path, out)/LoadENV(path)-- single-file helpersNewStore(opts)/NewStoreWithDB(db)/NewEnvOnlyStore()Store.GetValue,Store.SetIntValue,Store.SetBoolValue,Store.SetValue
Quick start
import "github.com/LingByte/ling-base/common/config"
type AppCfg struct {
Server struct {
Port int `yaml:"port" env:"SERVER_PORT"`
Host string `yaml:"host" env:"SERVER_HOST"`
} `yaml:"server"`
}
var cfg AppCfg
if err := config.Load("config/", "dev", &cfg); err != nil {
log.Fatal(err)
}License
MIT