Configuration manager with default Env, Json and Memory driver.
Mote: Config use Caster for get dependencies by type.
Config library contains three different driver by default.
Env driver use environment file (.env) for managing configuration.
import "github.com/gomig/config"
envConf, err := config.NewEnvConfig("app.env", "db.env", ".env")JSON driver use json file for managing configuration.
Caution: When you pass multiple file, for accessing config you must pass file name as first part of config path!
import "github.com/gomig/config"
jsonConf, err := config.NewJSONConfig("app.json", "db.json", "global.json")Use in-memory array for keeping and managing configuration.
import "github.com/gomig/config"
memConf, err := config.NewMemoryConfig(map[string]any{
"name": "My First App",
"key": "My Secret Key",
})Config interface contains following methods:
Load/Reload configurations.
// Signature:
Load() error
// Example
err := envConf.Load()Set configuration item. this function override preloaded config.
// Signature:
Set(key string, value any) error
// Example
err := memConf.Set("name", "My App")
err = envConf.Set("APP_NAME", "My App")
err = jsonConf.Set("app_name", "My App")Cation: For setting/overriding config item in JSON driver with multiple files pass filename as first part of config path.
import "github.com/gomig/config"
jsonConf, err := config.NewJSONConfig("file1.json", "file2.json")
err = jsonConf.Set("file1.app.title", "Some")Get configuration. Get function return config item as any. if you need get config with type use helper get functions described later.
// Signature:
Get(key string) anyCaution: For JSON driver with multiple file you must pass filename as first part of config path!
item := jsonConf.Get("file1.app.title")Check if config item exists.
// Signature:
Exists(key string) boolParse config as caster.
// Signature:
Cast(name string) caster.Caster
// Example:
v, err := conf.Cast("timeout").Int()