Skip to content

Commit

Permalink
Merge pull request #9 from wwwangxc/docs/example_config
Browse files Browse the repository at this point in the history
docs: example
  • Loading branch information
wwwangxc authored Nov 5, 2022
2 parents 706ee79 + 6070cfd commit ae8bf98
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions config/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package config_test

import (
"fmt"

"github.com/wwwangxc/gopkg/config"
)

func Example() {
// {
// "machine_id": 1,
// "local": {
// "env": "test",
// "login_name": "${LOGNAME}"
// }
// }
configure, err := config.Load("config.json", config.WithUnmarshaler("json"), config.WithWatchCallback(watch))
if err != nil {
fmt.Printf("load config fail. err:%v", err)
return
}
_ = configure.GetUint32("machine_id", 999) // return 1
_ = configure.GetString("local.env", "default") // return "test"
_ = configure.GetString("app", "default") // return "default"

// machine_id: 1
// local:
// env: test
// login_name: ${LOGNAME}
configure, err = config.Load("config.yaml", config.WithWatchCallback(watch))
// or
// configure, err = config.Load("config.yaml", config.WithUnmarshaler("yaml"), config.WithWatchCallback(watch))
if err != nil {
fmt.Printf("load config fail. err:%v", err)
return
}
_ = configure.GetUint32("machine_id", 999) // return 1
_ = configure.GetString("local.env", "default") // return "test"
_ = configure.GetString("app", "default") // return "default"

// machine_id=1
//
// [local]
// env_name="test"
// login_name="${LOGNAME}"
configure, err = config.Load("config.toml", config.WithUnmarshaler("toml"), config.WithWatchCallback(watch))
if err != nil {
fmt.Printf("load config fail. err:%v", err)
return
}
_ = configure.GetUint32("machine_id", 999) // return 1
_ = configure.GetString("local.env", "default") // return "test"
_ = configure.GetString("app", "default") // return "default"

}

func watch(configure config.Configure) {
// callback when file changed
}

0 comments on commit ae8bf98

Please sign in to comment.