Golang application config manage tool library.
- support multi format:
JSON(default),INI,YAML,TOML,HCLJSONcontent support comments. will auto clear comments
- support multi file/data load
- support for loading configuration data from remote URLs
- support for setting configuration data from command line arguments
- support data overlay and merge, automatically load by key when loading multiple copies of data
- support get sub value by path, like
map.keyarr.2 - support parse ENV name. like
envKey: ${SHELL}->envKey: /bin/zsh - generic api
GetIntStringBoolIntsIntMapStringsStringMap... - complete unit test(coverage > 90%)
If you just want to use INI for simple config management, recommended use gookit/ini
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
- val21examples code please see _examples/yaml.go:
package main
import (
"github.com/gookit/config"
"github.com/gookit/config/yaml"
)
// go run ./examples/yaml.go
func main() {
config.WithOptions(config.ParseEnv)
// add driver for support yaml content
config.AddDriver(yaml.Driver)
// config.SetDecoder(config.Yaml, yaml.Decoder)
err := config.LoadFiles("testdata/yml_base.yml")
if err != nil {
panic(err)
}
// fmt.Printf("config data: \n %#v\n", config.Data())
// load more files
err = config.LoadFiles("testdata/yml_other.yml")
// can also load multi at once
// err := config.LoadFiles("testdata/yml_base.yml", "testdata/yml_other.yml")
if err != nil {
panic(err)
}
}- get integer
age, ok := config.Int("age")
fmt.Print(ok, age) // true 100- Get bool
val, ok := config.Bool("debug")
fmt.Print(ok, age) // true true- Get string
name, ok := config.String("name")
fmt.Print(ok, name) // true inhere- Get strings(slice)
arr1, ok := config.Strings("arr1")
fmt.Printf("%v %#v", ok, arr1) // true []string{"val1", "val21"}- Get string map
val, ok := config.StringMap("map1")
fmt.Printf("%v %#v",ok, val) // true map[string]string{"key":"val2", "key2":"val20"}- Value contains ENV var
value, ok := config.String("shell")
fmt.Print(ok, value) // true /bin/zsh- Get value by key path
// from array
value, ok := config.String("arr1.0")
fmt.Print(ok, value) // true "val1"
// from map
value, ok := config.String("map1.key")
fmt.Print(ok, value) // true "val2"- Setting new value
// set value
config.Set("name", "new name")
name, ok = config.String("name")
fmt.Print(ok, name) // true "new name"LoadData(dataSource ...interface{}) (err error)LoadFlags(keys []string) (err error)LoadExists(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)
Bool(key string) (value bool, ok bool)Int(key string) (value int, ok bool)Int64(key string) (value int64, ok bool)Ints(key string) (arr []int, ok bool)IntMap(key string) (mp map[string]int, ok bool)Float(key string) (value float64, ok bool)String(key string) (value string, ok bool)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)
Set(key string, val interface{}, setByPath ...bool) (err error)
go test -cover
// contains all sub-folder
go test -cover ./...- Ini parse gookit/ini/parser
- Yaml parse go-yaml
- Toml parse go toml
- Data merge mergo
- gookit/ini ini config manage
MIT