Documentation
¶
Overview ¶
Package json use the https://github.com/json-iterator/go for parse json
Example ¶
config.WithOptions(config.ParseEnv)
// add Decoder and Encoder
config.AddDriver(Driver)
err := config.LoadFiles("testdata/json_base.json")
if err != nil {
panic(err)
}
fmt.Printf("config data: \n %#v\n", config.Data())
err = config.LoadFiles("testdata/json_other.json")
// config.LoadFiles("testdata/json_base.json", "testdata/json_other.json")
if err != nil {
panic(err)
}
fmt.Printf("config data: \n %#v\n", config.Data())
fmt.Print("get config example:\n")
name, ok := config.String("name")
fmt.Printf("get string\n - ok: %v, val: %v\n", ok, name)
arr1, ok := config.Strings("arr1")
fmt.Printf("get array\n - ok: %v, val: %#v\n", ok, arr1)
val0, ok := config.String("arr1.0")
fmt.Printf("get sub-value by path 'arr.index'\n - ok: %v, val: %#v\n", ok, val0)
map1, ok := config.StringMap("map1")
fmt.Printf("get map\n - ok: %v, val: %#v\n", ok, map1)
val0, ok = config.String("map1.key")
fmt.Printf("get sub-value by path 'map.key'\n - ok: %v, val: %#v\n", ok, val0)
// can parse env name(ParseEnv: true)
fmt.Printf("get env 'envKey' val: %s\n", config.DefString("envKey", ""))
fmt.Printf("get env 'envKey1' val: %s\n", config.DefString("envKey1", ""))
// set value
config.Set("name", "new name")
name, ok = config.String("name")
fmt.Printf("set string\n - ok: %v, val: %v\n", ok, name)
// if you want export config data
// buf := new(bytes.Buffer)
// _, err = config.DumpTo(buf, config.JSON)
// if err != nil {
// panic(err)
// }
// fmt.Printf("export config:\n%s", buf.String())
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var Decoder config.Decoder = func(data []byte, v interface{}) (err error) { s := config.StripJSONComments(string(data)) return parser.Unmarshal([]byte(s), v) }
Decoder for json
View Source
var Driver = &jsonDriver{config.JSON}
Driver for json
View Source
var Encoder config.Encoder = parser.Marshal
Encoder for json
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.