Documentation ¶
Overview ¶
Package gopkg/config is a componentized config plugin. It provides an easy way to load configuration files.
Example ¶
package main import ( "fmt" "github.com/wwwangxc/gopkg/config" ) func main() { // { // "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 }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrUnmarshalerNotExist unmarshaler not exist ErrUnmarshalerNotExist = fmt.Errorf("%s: unmarshaler not exist", packageName) // ErrConfigNotExist config not exist ErrConfigNotExist = fmt.Errorf("%s: config not exist", packageName) )
Functions ¶
This section is empty.
Types ¶
type Configure ¶
type Configure interface { // Unmarshal unmarshal config raw data Unmarshal(interface{}) error // IsExist check the key exist IsExist(string) bool // Get get value by key // // return defaultVal, when key not exist // k support key1.key2.key3 Get(string, interface{}) interface{} // GetString get string value by key // // return defaultVal, when key not exist // k support key1.key2.key3 GetString(string, string) string // GetBool get bool value by key // // return defaultVal, when key not exist // k support key1.key2.key3 GetBool(string, bool) bool // GetInt get int value by key // // return defaultVal, when key not exist // k support key1.key2.key3 GetInt(string, int) int // GetInt32 get int32 value by key // // return defaultVal, when key not exist // k support key1.key2.key3 GetInt32(string, int32) int32 // GetInt64 get int64 value by key // // return defaultVal, when key not exist // k support key1.key2.key3 GetInt64(string, int64) int64 // GetUint get uint value by key // // return defaultVal, when key not exist // k support key1.key2.key3 GetUint(string, uint) uint // GetUint32 get uint32 value by key // // return defaultVal, when key not exist // k support key1.key2.key3 GetUint32(string, uint32) uint32 // GetUint64 get uint64 value by key // // return defaultVal, when key not exist // k support key1.key2.key3 GetUint64(string, uint64) uint64 // GetFloat32 get float32 value by key // // return defaultVal, when key not exist // k support key1.key2.key3 GetFloat32(string, float32) float32 // GetFloat64 get float64 value by key // // return defaultVal, when key not exist // k support key1.key2.key3 GetFloat64(string, float64) float64 }
Configure ...
type LoadOption ¶
type LoadOption func(*configureImpl)
LoadOption load option for config
func WithUnmarshaler ¶
func WithUnmarshaler(name string) LoadOption
WithUnmarshaler assign unmarshaler
func WithWatchCallback ¶
func WithWatchCallback(callback func(Configure)) LoadOption
WithWatchCallback with watch callback
Directories ¶
Path | Synopsis |
---|---|
example
|
|
Package mockconfig is a generated GoMock package.
|
Package mockconfig is a generated GoMock package. |
Click to show internal directories.
Click to hide internal directories.