本文为转载,原文地址:Beego学习笔记——Configgit
这是一个用来解析文件的库,它的设计思路来自于database/sql
,目前支持解析的文件格式有ini、json、xml、yaml,能够经过以下方式进行安装:github
go get github.com/astaxie/beego/config
首先初始化一个解析器对象sql
iniconf, err := NewConfig("ini", "testini.conf") if err != nil { t.Fatal(err) }
而后经过对象获取数据json
iniconf.String("appname")
解析器对象支持的函数有以下:app
Set(key, val string) error String(key string) string Int(key string) (int, error) Int64(key string) (int64, error) Bool(key string) (bool, error) Float(key string) (float64, error) DIY(key string) (interface{}, error)
appname="beegotest" pageoffset=20
package utils import ( "github.com/astaxie/beego/config" "fmt" ) var BConfig config.Configer func init(){ var err error BConfig, err = config.NewConfig("ini", "app.conf") if err != nil{ fmt.Println("config init error:", err) } }
func GetConfig(args []string)int{ if args[1] == "-int"{ intConf, err := utils.BConfig.Int(args[2]) if err != nil{ fmt.Println("get type of int from config error:",err) }else { fmt.Println(intConf) } }else if args[1] == "-int64"{ int64Conf, err := utils.BConfig.Int64(args[2]) if err != nil{ fmt.Println("get type of int64 from config error:",err) }else { fmt.Println(int64Conf) } }else if args[1] == "-bool"{ boolConf, err := utils.BConfig.Bool(args[2]) if err != nil{ fmt.Println("get type of bool from config error:",err) }else { fmt.Println(boolConf) } }else if args[1] == "-float"{ floatConf, err := utils.BConfig.Float(args[2]) if err != nil{ fmt.Println("get type of flaot from config error:",err) }else { fmt.Println(floatConf) } }else if args[1] == "-int64"{ intConf, err := utils.BConfig.Int(args[2]) if err != nil{ fmt.Println("get type of int from config error:",err) }else { fmt.Println(intConf) } }else { fmt.Println(utils.BConfig.String(args[2])) } return 0 }
func SetConfig(args []string)int{ err := utils.BConfig.Set(args[1], args[2]) if err != nil{ fmt.Println("set config error:", err) }else{ fmt.Println("set config success") } return 0 }
main.go文件中也须要作适当的调整,来调用上面两个函数。完整的代码以下:
package main import ( "bufio" "fmt" "os" "strings" _"beegotest/utils" "beegotest/utils" ) func main() { r := bufio.NewReader(os.Stdin) handlers := GetCommandHandlers() Help(nil) for { fmt.Print("Command> ") b, _, _ := r.ReadLine() line := string(b) tokens := strings.Split(line, " ") if handler, ok := handlers[tokens[0]]; ok{ ret := handler(tokens) if ret != 0{ break } }else { fmt.Println("Unknown Command:", tokens[0]) } } } func GetCommandHandlers() map[string]func(args []string) int { return map[string]func([]string) int{ "help": Help, "h": Help, "quit" : Quit, "q":Quit, "getconf":GetConfig, "setconf":SetConfig, } } func Help(args []string) int { fmt.Println(`Command: help(h) quit(q) getconf -type key setconf key val `) return 0 } func Quit(args []string) int{ return 1 } func GetConfig(args []string)int{ if args[1] == "-int"{ intConf, err := utils.BConfig.Int(args[2]) if err != nil{ fmt.Println("get type of int from config error:",err) }else { fmt.Println(intConf) } }else if args[1] == "-int64"{ int64Conf, err := utils.BConfig.Int64(args[2]) if err != nil{ fmt.Println("get type of int64 from config error:",err) }else { fmt.Println(int64Conf) } }else if args[1] == "-bool"{ boolConf, err := utils.BConfig.Bool(args[2]) if err != nil{ fmt.Println("get type of bool from config error:",err) }else { fmt.Println(boolConf) } }else if args[1] == "-float"{ floatConf, err := utils.BConfig.Float(args[2]) if err != nil{ fmt.Println("get type of flaot from config error:",err) }else { fmt.Println(floatConf) } }else if args[1] == "-int64"{ intConf, err := utils.BConfig.Int(args[2]) if err != nil{ fmt.Println("get type of int from config error:",err) }else { fmt.Println(intConf) } }else { fmt.Println(utils.BConfig.String(args[2])) } return 0 } func SetConfig(args []string)int{ err := utils.BConfig.Set(args[1], args[2]) if err != nil{ fmt.Println("set config error:", err) }else{ fmt.Println("set config success") } return 0 }
运行结果以下:ide