config
Golang application config manage tool library.
中文说明
- Support multi format:
JSON
(default), INI
, YAML
, TOML
, HCL
JSON
content support comments. will auto clear comments
- Support multi-file and multi-data loading
- Support for loading configuration data from remote URLs
- Support for setting configuration data from command line arguments(
flags
) - Support data overlay and merge, automatically load by key when loading multiple copies of data
- Support get sub value by path, like
map.key
arr.2
- Support parse ENV name. like
envKey: ${SHELL}
-> envKey: /bin/zsh
- Generic api
Get
Int
String
Bool
Ints
IntMap
Strings
StringMap
... - complete unit test(code coverage > 95%)
Only use INI
If you just want to use INI for simple config management, recommended use gookit/ini
GoDoc
Usage
Here using the yaml format as an example(testdata/yml_other.yml
):
name: app2
debug: false
baseKey: value2
shell: ${SHELL}
envKey1: ${NotExist|defValue}
map1:
key: val2
key2: val20
arr1:
- val1
- val21
Load data
examples code please see _examples/yaml.go:
package main
import (
"github.com/gookit/config"
"github.com/gookit/config/yaml"
)
func main() {
config.WithOptions(config.ParseEnv)
config.AddDriver(yaml.Driver)
err := config.LoadFiles("testdata/yml_base.yml")
if err != nil {
panic(err)
}
err = config.LoadFiles("testdata/yml_other.yml")
if err != nil {
panic(err)
}
}
Read data
age, ok := config.Int("age")
fmt.Print(ok, age)
val, ok := config.Bool("debug")
fmt.Print(ok, val)
name, ok := config.String("name")
fmt.Print(ok, name)
arr1, ok := config.Strings("arr1")
fmt.Printf("%v %#v", ok, arr1)
val, ok := config.StringMap("map1")
fmt.Printf("%v %#v",ok, val)
value, ok := config.String("shell")
fmt.Print(ok, value)
value, ok := config.String("arr1.0")
fmt.Print(ok, value)
value, ok := config.String("map1.key")
fmt.Print(ok, value)
config.Set("name", "new name")
name, ok = config.String("name")
fmt.Print(ok, name)
API Methods Refer
Load Config
LoadData(dataSource ...interface{}) (err error)
Load from struts or mapsLoadFlags(keys []string) (err error)
Load from cli flagsLoadExists(sourceFiles ...string) (err error)
LoadFiles(sourceFiles ...string) (err error)
LoadRemote(format, url string) (err error)
LoadSources(format string, src []byte, more ...[]byte) (err error)
LoadStrings(format string, str string, more ...string) (err error)
Getting Values
DefXXX
get value with default value
Bool(key string) (value bool, ok bool)
DefBool(key string, defVal ...bool) bool
Int(key string) (value int, ok bool)
DefInt(key string, defVal ...int) int
Int64(key string) (value int64, ok bool)
DefInt64(key string, defVal ...int64)
Ints(key string) (arr []int, ok bool)
IntMap(key string) (mp map[string]int, ok bool)
Float(key string) (value float64, ok bool)
DefFloat(key string, defVal ...float64) float64
String(key string) (value string, ok bool)
DefString(key string, defVal ...string) string
Strings(key string) (arr []string, ok bool)
StringMap(key string) (mp map[string]string, ok bool)
Get(key string, findByPath ...bool) (value interface{}, ok bool)
Setting Values
Set(key string, val interface{}, setByPath ...bool) (err error)
Useful Methods
AddDriver(driver Driver)
Data() map[string]interface{}
DumpTo(out io.Writer, format string) (n int64, err error)
Run Tests
go test -cover
// contains all sub-folder
go test -cover ./...
Related Packages
Ini Config Use
License
MIT